我有 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/