Express.js sessions activated on a subset of routes

陌路散爱 提交于 2019-12-01 09:19:25

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!