Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
Trouble opening localhost with Node and mssql

I'm using Node to connect to a Microsoft SQL Developer database. I've finally gotten my code to run without errors:

var sql = require('mssql/msnodesqlv8');
const express = require('express');
const app = express();


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


// Config your database credential
const config = {
    server: "xxxx",
    driver:"xxxx",
    database: "xxxx",
    user: "xxxx",
    password: "xxxx",
    options:{
    trustServerCertificate: true,
}
};

// Connect to your database
new sql.ConnectionError(config,function(err){

    // Create Request object to perform
    // query operation
    var request = new sql.Request();

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

            if (err) console.log(err)

            // Send records as a response
            // to browser
            res.send(records);

        });
    });
});


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

But, when I go to :

http://localhost:5000/

It doesn't load, it says the page cannot be reached. What can I try to resolve this?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You're using the wrong thing to try and connect to SQL Server. You don't use new sql.ConnectionError(), you use sql.connect(). This error is causing your app to crash so nothing is listening on port 5000.

var sql = require('mssql/msnodesqlv8');
const express = require('express');
const app = express();


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


// Config your database credential
const config = {
    server: "xxxx",
    driver:"xxxx",
    database: "xxxx",
    user: "xxxx",
    password: "xxxx",
    options:{
    trustServerCertificate: true,
}
};

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

    // Create Request object to perform
    // query operation
    var request = new sql.Request();

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

            if (err) console.log(err)

            // Send records as a response
            // to browser
            res.send(records);

        });
    });
});


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

Run that (after having applied proper database connection configuration values) and then you should be able to open your browser and connect to http://localhost:5000

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!