[英]Why can i not connect to NodeJS express server from any network?
[英]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.