簡體   English   中英

為什么我無法使用 NodeJS 連接到 SQLServer?

[英]Why can't I connect to SQLServer uisng NodeJS?

我是使用 Node JS 的新手,現在我正在創建一個連接到 SQL Server 的項目,但是當我使用命令node Database\\connect.js它什么也不做,因為它應該執行它所做的 console.log連接或沒有。

這是我的package.json

{
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "tedious": "^14.0.0"
  },
  "name": "weather-nodejs-apirest",
  "version": "1.0.0",
  "main": "connect.js",
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

這是我的connect.js

var Connection = require('tedious').Connection;
var config = {
    server: 'SERVERNAME',
    authentication: {
        type: 'default',
        options: {
            userName: 'sa',
            password: 'password'
        }
    },
    options: {
        database: 'weather_app',
        // instanceName: 'Sqlexpress',
        rowCollectionOnDone: true,
        useColumnNames: false
    }
}

var connection = new Connection(config);
connection.on('connect', function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Connected');
    }
});

module.exports = connection;

connection.on(...)方法不會啟動數據庫連接本身。 它僅在應用程序使用connection.connect()方法啟動它時運行。

由於您是從connect.js向外部導出connection ,因此您應該在某處(主要在應用程序入口點)導入並啟動連接,

const connection = require('path/to/connect.js');

// initiate
connection.connect();

文檔: http : //tediousjs.github.io/tedious/getting-started.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM