Chaining multiple pieces of middleware for specific route in ExpressJS

前端 未结 3 1255
我寻月下人不归
我寻月下人不归 2020-12-07 20:02

I want to just verify something but have\'t been able to find anything in the Express docs or online regarding this (although I know it\'s a feature).

I could just t

3条回答
  •  佛祖请我去吃肉
    2020-12-07 20:35

    Consider following example:

    var 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 above piece of code, you can see that "requireAuthentication" and "logger" are two different middlewares added.

提交回复
热议问题