Catch all route EXCEPT for /login

前端 未结 4 1200
耶瑟儿~
耶瑟儿~ 2020-12-13 05:16

I am currently writing an API which will require a user to pass an authentication token in the header of each request. Now I know I can create a catchall route say

4条回答
  •  清歌不尽
    2020-12-13 06:02

    You can always place catch-all route after the ones you want to exclude (see robertklep answer).

    But sometimes you simply don't want to care about the order of your routes. In this case you still can do what you want:

    app.get('*', function(req, res, next) {
      if (req.url === '/' || req.url === '/login') return next();
      ...
    });
    

提交回复
热议问题