Express.js execution flow using callbacks

I have a huge doubt about the execution flow of an express.js application in which there are asynchronous requests to a database. I have read about the Node.js architecture and I know that when I perform a blocking request (like a db request) the request is performed on a thread that, when the request is satisfied, add work (the code of the callback function that was specified for the blocking request) on the event queue. Now... since I'm using mongoDb as application database and since mongodb doesn't provide methods for performing synchronous requests, how can I be sure that the response to the client is not performed before the query has been completed?

for example, in this situation in which I have a request handler (not a middleware):

app.all("/",function(req,res){

    db.find({},function(err,doc){

     //Retreive certain informations from the db needed by the client

    });
});

app.all("/",function(req,res){
   res.status(200).end();
});

This is a common example of use of express.js and mongodb...

But how the execution flow actually goes on?

For the middlewares I don't have this doubt because the execution flow stops until the next() method is called (and you can call it inside the database callback).

Thanks in advance,

Luca M.

Request will be open until you send something back. Simply respond inside the database query callback, like so:

app.all("/",function(req,res){

    db.find({},function(err,doc){

        if(err) {
            //handle error
            console.error('oops', err);
            res.send(500);
        } else {
            res.json(200, doc);
        }
    });
});

In this case, the order in which your routes are defined matter. In app.all, you can actually specify the next() function to perform those calls sequentially.

http://expressjs.com/api.html#app.all

Since you mentioned next() here is a basic example of how to chain middlewares:

function findInDb(id, callback){
  db.get(id, function(err, data){
    if(err){ return callback(err) };
    var obj = JSON.parse(data)[0] // or something
    callback(null, obj)
  });
};

app.all('*', function(req, res, next){
  findInDb('something', function(err, obj){
    if(err){ return next(err) };
    res.myObj = obj;
    next();
  });
});

app.get('/', function(req, res){
  // By now the first middleware executed
  res.status(200).json(res.myObject);
});