Express.js execution flow using callbacks

11,367

Solution 1

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);
});

Solution 2

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);
        }
    });
});

Solution 3

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

Share:
11,367

Related videos on Youtube

Luca Marzi
Author by

Luca Marzi

Studying computer science at the Politecnico di Milano. I have developed an app called "Liukbar" (google it :)) You can find more information about experience: lucamarzi.wordpress.com

Updated on June 04, 2022

Comments

  • Luca Marzi
    Luca Marzi almost 2 years

    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.

  • Plato
    Plato about 9 years
    Yep this is the general node pattern. if you have to wait to call a function until something asynchronous completes, you do it within the callback to that async method (in this case, db.find)
  • Luca Marzi
    Luca Marzi about 9 years
    This is exactly the same solution that I was thinking about. Thanks.
  • Luca Marzi
    Luca Marzi about 9 years
    Yes, but this is the doubt: "I call db.find() so I'm performing a blocking request...so a thread is assigned to the request and the main execution goes on"
  • Luca Marzi
    Luca Marzi about 9 years
    But what does"return next(err)" mean?
  • Plato
    Plato about 9 years
    when you invoke next(err) it tells Express that there was a failure in your code. It will bypass any subsequent normal express middleware and go directly to the first error handling middleware. The return statement in front isn't strictly necessary; I just like to explicitly see in my code that no further execution will happen in this function.
  • Plato
    Plato about 8 years
    btw for custom error handling middleware: have 4-ary function signatures, put it after other routes: app.use(function(err, req, res, next) { ...