Chaining multiple pieces of middleware for specific route in ExpressJS

Solution 1:

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('/', [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.

Solution 2:

It's not saying "infinitely", but it does say that you can add multiple middleware functions (called "callbacks" in the documentation) here:

router.METHOD(path, [callback, ...] callback)

...

You can provide multiple callbacks, and all are treated equally, and behave just like middleware, except that these callbacks may invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to perform pre-conditions on a route then pass control to subsequent routes when there is no reason to proceed with the route matched.

As you can see, there's not distinction between a middleware function and the function that commonly handles the request (the one which is usually the last function added to the list).

Having 10 shouldn't be a problem (if you really need to).