Express.js sessions activated on a subset of routes

前端 未结 1 805
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 04:08

I\'m working with expressjs and want to authenticate users for a login using sessions. The site/app should on one hand allow the user to browse and investigate different pro

相关标签:
1条回答
  • 2021-01-14 04:43

    You can wrap middleware in a function to black or whitelist certain urls. This is a whitelist example:

    function allow(paths, fn) {
      return function(req, res, next) {
        if (~paths.indexOf(req.url)) {
          return fn(req, res, next);
        }
        next();
      }
    }
    

    You wrap it around your middleware like:

    app.use(allow(['/bibliotek', '/registrer'], express.cookieParser()));
    

    You can use the same technique for other middleware as well. This was based on a gist that the express author showed in irc.

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