5

I am new to node.js and need help connecting with SQL server

I have the following node.js code

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: 'Amtex123',
        server: 'LAPTOP-7ACI67I5', 
        database: 'TData',
        port: 1433
    };

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

        if (err) console.log(err);
    enter code here
        // create Request object
        var request = new sql.Request();

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

            if (err) console.log(err)
            // send records as a response
            res.send(recordset);  
           });
         });
      });
var server = app.listen(3000, function () {
    console.log('Server is running..');
});

I am getting an error messages

ConnectionError: Failed to connect to LAPTOP-7ACI67I5:1433 - Could not connect (sequence)

at Connection.tedious.once.err (C:\Projects\nodejs\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.socketError (C:\Projects\nodejs\node_modules\tedious\lib\connection.js:869:14)
at C:\Projects\nodejs\node_modules\tedious\lib\connection.js:739:25
at SequentialConnectionStrategy.connect (C:\Projects\nodejs\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (C:\Projects\nodejs\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
code: 'ESOCKET',**

I checked various solution to and

  1. all SQL Services are running
  2. TCP/IP is enabled
2
  • I got it working. Changed the TCP Dynamic ports to blank (instead of zero)
    – Pradeep
    Dec 15, 2017 at 19:07
  • Which version of sql server are you using?
    – FrenkyB
    Nov 11, 2019 at 21:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.