javascript - session token 过期时如何将用户重定向到登录页面?

标签 javascript node.js express

我有 Marionette + Node 应用程序。 当 token 过期时,应用程序不会使用react,用户也不会重定向到登录页面。 我的问题是 - 如何从 Node 监听 session token 状态?

最佳答案

您好,先生。让我快速介绍一下请求处理程序、端点和中间件。

Express.js 是非常常见的请求处理程序。请求处理程序,做他们听起来像做的事。他们处理请求;更具体的http请求。您可以在线找到大量有关如何使用 express.js 创建基本端点的示例。

现在进入更重要的部分,中间件。在 express 中,至少中间件是插入到达请求和它要到达的终点之间的软件。

我将使用 Express 语法。

假设我有一个端点 foo:

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

但是,此端点只能在特定条件下访问。所以我在该请求处理程序定义中插入了一个中间件:

    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) {})

中间件的使用量很大。 Google express 中间件,您会找到大量信息。

祝你好运。

关于javascript - session token 过期时如何将用户重定向到登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785394/

相关文章:

javascript - 模块化JS : global functionality on 3 sections (checkboxes and radios) & Sholud I apply "rendering" in my code?

javascript - 带 Twitter bootstrap 的水平滚动表

node.js - 用于连接到 InDesign Server 的 Node SOAP 客户端

javascript - 映射数组返回 Unresolved promise

javascript - 如果 redux 更新了嵌套组件,但 PureComponent 作为 prop 传递给了它的父组件,那么 PureComponent 会重新渲染吗?

javascript - 更改 react 路由器链接的顺序

javascript - 使用 axios.get 时套接字挂起,但使用 https.get 时不会

node.js - next() 在 Express 4 中不起作用

javascript - 位置 1 的 JSON 中的意外标记 u

node.js - 在jade中快速上传图片并查看