简体   繁体   English

特定路线的快速设置会话

[英]Express Set Session for Specific Routes

I am trying to include sessions only for some routes (the authentication ones) but I am having a problem because of the error page routes:我正在尝试仅包括某些路由(身份验证路由)的会话,但由于错误页面路由,我遇到了问题:

I have this:我有这个:

    app.use(session({
        secret: config.secrets.session,
        saveUninitialized: false,
        resave: false,
        store: sessionStore,
        proxy: true,
        cookie: {
            maxAge: config.token_duration,
            secure: false
        }
        // rolling: false
    }));


    app.use('/api/user', require('./api/user'));
    app.use('/api/auth', require('./api/auth'));  

    app.route(['/error/500','/error/404','/user/settings'])
        .get((req, res) => {
            res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
        });

    app.route('/*/*')
        .get(errors[404]);       

    app.use(errors[500]); 

So, If I use it like this, all the pages in my application will create a session (which I don't want).所以,如果我这样使用它,我应用程序中的所有页面都会创建一个会话(我不想要)。 If I move the session section after the error routes, I will never get to the api routes because it will reach the 404 route.如果我在错误路由之后移动会话部分,我将永远无法到达 api 路由,因为它将到达 404 路由。

Thanks in advance提前致谢

Middleware can be associated with only certain routes and the order in which it is specified matters.中间件只能与某些路由相关联,并且指定的顺序很重要。 There are a number of ways to do that and how to best implement it depends upon the paths your site uses and how you can most easily create a link between path and whether it should or should not have the session middleware on it.有多种方法可以做到这一点,如何最好地实现它取决于您的站点使用的路径以及如何最轻松地在路径之间创建链接以及它是否应该在其上包含会话中间件。

One simple thing to do would be to put your error route handlers BEFORE your session middleware.一件简单的事情就是将错误路由处理程序放在会话中间件之前。 Then, those route handlers would "handle" the request first and the session middleware would never get called.然后,那些路由处理程序将首先“处理”请求,并且永远不会调用会话中间件。

app.route(['/error/500','/error/404','/user/settings'])
    .get((req, res) => {
        res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
    });

app.use(session({
    secret: config.secrets.session,
    saveUninitialized: false,
    resave: false,
    store: sessionStore,
    proxy: true,
    cookie: {
        maxAge: config.token_duration,
        secure: false
    }
    // rolling: false
}));

Other things you can do:您可以做的其他事情:

  1. Put a path on your session middleware so it is only invoked for certain paths in your site (all authenticated pages should be below that path).在您的会话中间件上放置一个路径,以便仅针对您站点中的某些路径调用它(所有经过身份验证的页面都应位于该路径下方)。

  2. Create your own middleware handler that checks to see if the path is /error and if not, then it invokes the session middleware handler.创建您自己的中间件处理程序,检查路径是否为/error ,如果不是,则调用会话中间件处理程序。 If it is /error , then don't invoke the session middleware.如果是/error ,则不要调用会话中间件。

This last one could be done like this:最后一个可以这样做:

const sessionHandler = session({
    secret: config.secrets.session,
    saveUninitialized: false,
    resave: false,
    store: sessionStore,
    proxy: true,
    cookie: {
        maxAge: config.token_duration,
        secure: false
    }
    // rolling: false
});

app.use(function(req, res, next) {
    // if path does not start with /error/, then invoke session middleware
    if (req.url.indexOf("/error/") !== 0) {
        return sessionHandler(req, res, next);
    } else {
        next();
    }
});

You can also chain/compose multiple middleware using Express.router() as mentioned in this example: Express: composing middleware您还可以使用 Express.router() 链接/组合多个中间件,如本例中所述: Express: composing middleware

And then apply that composed router (of chained middleware) as a specific route handler.然后将该组合路由器(链式中间件)应用为特定的路由处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM