Chaining multiple pieces of middleware for specific route in ExpressJS

Consider following example:

const middleware = {
    requireAuthentication: function(req, res, next) {
        console.log('private route list!');
        next();
    },
    logger: function(req, res, next) {
       console.log('Original request hit : '+req.originalUrl);
       next();
    }
}

Now you can add multiple middleware using the following code:

app.get("https://stackoverflow.com/", [middleware.requireAuthentication, middleware.logger], function(req, res) {
    res.send('Hello!');
});

So, from the above piece of code, you can see that requireAuthentication and logger are two different middlewares added.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)