Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda