in express how multiple callback works in app.get

前端 未结 1 345
予麋鹿
予麋鹿 2020-12-15 21:48

I am newbie in node so please forgive me if i am not getting obvious. In node.js express application for app.get function we typically pass route and view as parameters e.g

1条回答
  •  时光说笑
    2020-12-15 22:12

    In Express, each argument after the path is called in sequence. Usually, this is a way of implementing middleware (as you can see in the example you provided).

    app.get('/users', middleware1, middleware2, middleware3, processRequest);
    
    function middleware1(req, res, next){
        // perform middleware function e.g. check if user is authenticated
    
        next();  // move on to the next middleware
    
        // or
    
        next(err);  // trigger error handler, usually to serve error page e.g. 403, 501 etc
    }
    

    0 讨论(0)
提交回复
热议问题