简体   繁体   English

会话令牌过期后如何将用户重定向到登录页面?

[英]How to redirect user to login page when session token expired?

I have Marionette + Node application. 我有Marionette + Node应用程序。 When token expires the app does not react and user is not redirects to the LogIn page. 令牌到期后,应用程序将不响应,用户也不会重定向到“登录”页面。 My question is - how to listen for session token status from Node? 我的问题是-如何从Node监听会话令牌状态?

Good day to you sir. 先生,你好。 Let me give you a quick intro to request handlers, endpoints, and middleware. 让我快速介绍一下请求处理程序,端点和中间件。

Express.js is very common request handler. Express.js是非常常见的请求处理程序。 Request handlers, do what they sound like they do. 请求处理程序,按其听起来的方式做。 They handle requests; 他们处理请求; more specifically http requests. 更具体地说是http请求。 You can find plenty of examples online on how to create basic endpoints with express.js. 您可以在网上找到大量有关如何使用express.js创建基本端点的示例。

Now on to the more important part, middleware. 现在到更重要的部分,中间件。 In express at least middleware is software that's inserted between the arriving request, and the end point it was meant to reach. 至少在中间件中,中间件是在到达的请求和到达的终点之间插入的软件。

I will use Express syntax. 我将使用Express语法。

Say I have an endpoint foo: 假设我有一个端点foo:

    Router.get('/foo', function(req, res) {});

However this endpoint should only be accessible under certain conditions. 但是,仅在某些条件下才可以访问此端点。 So I insert a middleware right in that request handler definition: 因此,我在该请求处理程序定义中插入了一个中间件:

    Router.get('/foo', function iAmAMiddleware(req, res, next) {
      Here you can implement any logic you want. you have access to 
      the request, and the response. Meaning that if something in wrong
      in the request, then you can return a response from here like 

      res.send(404);


      BUT if all checks out all you have to do is call next() and 
      the flow will continue into the actual handler function. 
    },
    function iAmTheEndpointHandler(req, res) {})

Usage of middleware is huge. 中间件的使用量巨大。 Google express middleware and you'll find plenty of information. Google Express中间件,您会发现很多信息。

Good luck to you. 祝你好运。

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

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