我是使用 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;
uj5u.com熱心網友回復:
該connection.on(...)方法不會啟動資料庫連接本身。它僅在應用程式使用connection.connect()方法啟動它時運行。
由于您是connection從匯出到外部connect.js,您應該在某處匯入并啟動連接(主要是在應用程式入口點),
const connection = require('path/to/connect.js');
// initiate
connection.connect();
檔案: http : //tediousjs.github.io/tedious/getting-started.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/347865.html
