done_merson
done_merson

Reputation: 2998

Why does the script get stuck on the connection in this nodejs script

I am using the following from http://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs:

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: 'mypassword',
        server: 'localhost', 
        database: 'SchoolDB' 
    };

    // 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 Student', 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..');
});

When I run this file in Browser, the first time the page runs. But if I refresh it, it says the connection is not open. When I run this outside of the webserver context, it never exits the sql.connect function and needs to be stopped with Control-C in Node. Does anyone know why this code gets stuck in the sql.connect function?

Upvotes: 0

Views: 557

Answers (1)

Sebastian Hildebrandt
Sebastian Hildebrandt

Reputation: 2791

First: The example you where using are based on version 2.3 of node-mssql. The current version ist now 4.1. One recommended way of using node-mssql is using connectionPools. I adapted your code to use pools.

Second: If there is an error, in your code you never reach the point res.send(). So I modified your code to send somethig back in case of an error.

One more hint: I would place the dependency at the top of your app (not within the route)... it will work either way, but your code gets a little bit more clear.

'use strict';

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

// config for your database
const config = {
    user: 'sa',
    password: 'mypassword',
    server: 'localhost', 
    database: 'SchoolDB',
    options: {
        encrypt: false
    },
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 30000
    }    
};

// create a connection pool
const pool = new sql.ConnectionPool(config, err => {
    if (err) {
        console.log(err);
    }
});

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

    // create Request object (using the connection pool)
    const request = new sql.Request(pool);

    // query to the database and get the records
    request.query('select * from Student', (err, recordset) => {
        if (err) {
            console.log(err);
            res.send(err);
        } else {
            // send records as a response
            res.send(recordset);
        }
    });
});

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

Hope that helps (code above is not tested ...)

Upvotes: 1

Related Questions