无法将NodeJS(带有Express)连接到SQL Server 2017

时间:2018-12-03 09:14:01

标签: node.js sql-server

我尝试使用NodeJS(Express)连接到SQL Server 2017 Developer,但出现错误。第一个问题是我不确定连接配置(用户名,密码等)是否正确。其次是方法如何连接它。

我浏览了网络,得到了这样的东西:

var express = require('express');
var app = express();

app.get('/', function (req, res) {

    var sql = require("mssql");

    // config for your database
    var config = {
        user: 'sa',
        password: 'password',
        server: 'localhost', 
        database: 'database_name',
    };

    // connect to your database
    sql.connect(config, function (err) {

        if (err) console.log(err);

        // create Request object
        var request = new sql.Request();

        // query to the database and get the records
        request.query('select * from dbo.Products', function (err, recordset) {

            if (err) console.log(err)

            // send records as a response
            res.send(recordset);

        });
    });
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
});

但是在服务器上出现此错误:

Server is running..
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
    at Connection.tedious.once.err (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\mssql\lib\tedious.js:239:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.socketError (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connection.js:1024:14)
    at F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connection.js:868:25
    at SequentialConnectionStrategy.connect (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connector.js:154:9)
    at Socket.onError (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connector.js:170:16)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
  code: 'ESOCKET',
  originalError:
   { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
    at ConnectionError (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\errors.js:12:12)
    at Connection.socketError (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connection.js:1024:30)
    at F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connection.js:868:25
    at SequentialConnectionStrategy.connect (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connector.js:154:9)
    at Socket.onError (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\tedious\lib\connector.js:170:16)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
     message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
     code: 'ESOCKET' },
  name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
    at Request._query (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\mssql\lib\base.js:1344:37)
    at Request._query (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\mssql\lib\tedious.js:531:11)
    at Request.query (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\mssql\lib\base.js:1280:12)
    at F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\server.js:25:17
    at _poolCreate.then.catch.err (F:\upwork\Bobby - Integrate MS SQL with NodeJS\ticketapp\node_modules\mssql\lib\base.js:276:7)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7) code: 'ECONNCLOSED', name: 'ConnectionError' }

在哪里可以获得用户名和密码信息? 我是否需要运行MS SQL Server才能进行连接(例如通过默认端口1433)?如果可以,该如何运行?

0 个答案:

没有答案