简体   繁体   中英

SQL Server connection issue from node js application

Getting below issue when I am trying to connect to SQL Server from Node Js application.

The SQL Server name used DESKTOP-FCM7L54\\SRINIVAS is the correct one.

Error details:

{ ConnectionError: Failed to connect to DESKTOP-FCM7L54SRINIVAS:1433 - getaddrinfo ENOTFOUND DESKTOP-FCM7L54SRINIVAS

at Connection.base.ConnectionPool._poolCreate.base.Promise.tedious.once.err (C:\\Users\\Owner\\documents\\visual studio 2015\\Projects\\NodejsConsoleApp3\\SqlNodejs\\node_modules\\mssql\\lib\\tedious.js:216:17)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (C:\\Users\\Owner\\documents\\visual studio 2015\\Projects\\NodejsConsoleApp3\\SqlNodejs\\node_modules\\tedious\\lib\\connection.js:699:14)
at C:\\Users\\Owner\\documents\\visual studio 2015\\Projects\\NodejsConsoleApp3\\SqlNodejs\\node_modules\\tedious\\lib\\connection.js:590:25
at C:\\Users\\Owner\\documents\\visual studio 2015\\Projects\\NodejsConsoleApp3\\SqlNodejs\\node_modules\\tedious\\lib\\connector.js:68:18
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:89:17)
Code: 'ESOCKET',

var express = require('express');
var app = express();
var sql = require("mssql");

// config for your database
var config = {
    server: 'DESKTOP-FCM7L54\SRINIVAS', 
    database: 'Test',
    user: 'sa',
    password: 'test',
    port: 1433
};

app.get('/', function (req, res) {
     //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 Customer', function (err, recordset) {
            if (err) console.log(err)
            // send records as a response
            res.send(recordset);
        });
    });
    sql.close();
});

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

The error code you are getting is ENOTFOUND , which means the connection is not found. Verify your host and port, and that the service is indeed running on this port.
One more think you can see in your error - it says getaddrinfo ENOTFOUND DESKTOP-FCM7L54SRINIVAS - notice the missing backslash in your name, might be escaping it. Maybe try connecting to DESKTOP-FCM7L54\\\\SRINIVAS

Another thing I'm seeing in your code, not related to this problem - you are not handling the errors correctly - you print them to the console, but not stopping the flow of your code, which can cause some undesired behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM