A
A
Alexey2018-03-10 18:05:51
Node.js
Alexey, 2018-03-10 18:05:51

Why doesn't Node js return static until it completes a database request?

Node js does not return static (and does not respond to any requests at all) until it completes the request to the database.
On the local computer under windows 7 everything works well, the problem is observed when transferring to windows server 2008 r2.
The application itself is running on windows server 2008 r2
database on sq server.
I use express as server and mssql for database.
the code

const express = require('express');
const mssql = require('mssql');
const serveStatic = require ('serveStatic');


app.use(serveStatic('./public'));

app.listen(3000);

const dbConfig =  {
    driver: "tedious",
    server: "server",
    database: "database",
    user: "Login",
    password: "..."
};

var connection = new mssql.connect(dbConfig);

app.get('/test',  function(req, res) {
    
    let sql = `select *   FROM [dbo].[table]  WITH(NOLOCK)`;

    connection.then(function(){

        var request=new mssql.Request();
        request.input('user',req.session.user);
        request.query(sql).then(

            function(rec){
                if(rec.recordset.length===0){
                    res.status(400).send('По запросу данных не найденно');
                }else {
                    res.send(rec);
                }

            }
        ).catch(function(err){
            console.dir(err);
            res.status(500).send('Ошибка запроса');
        });

    })

})

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

    res.send('Ни чего не верну пока отрабатывается запрос test')
    
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Filippenko, 2018-03-10
@alexfilus

Because nodeJS is single-threaded, and the query to the database is executed synchronously.
Try rewriting it so that the request is asynchronous.
https://www.npmjs.com/package/mssql#quick-example

B
bro-dev, 2018-03-17
@bro-dev

Well, I had this because the percent just died at 101% load, and didn’t answer anything either, it wasn’t connected with the base, there were just heavy isolations. Just try to output the time to the console by interval and see how stable it does it.
and better first remember time=new Date.now()
in the console output time-new Date.now() and save again. or in pm2 run and see how much the average time of the event loop is there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question