Prevent Expressjs from creating a session when requests contain an authorization header?

前端 未结 4 858
野的像风
野的像风 2021-02-06 04:26

I have an API that can be called either using a browser where requests are transactional and have a session OR directly, eg. using curl, where requests are atomic. Browser reque

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 05:09

    Try this:

    var sessionMiddleware = exp.session( session_options );
    
    app.use(function(req, res, next) {
      if (req.headers.authorization) {
        return next();
      }
      return sessionMiddleware(req, res, next);
    });
    

提交回复
热议问题