我可以通过 express next() 函数发送数据吗? [英] Can I send data via express next() function?
问题描述
我正在开发一个需要使用 express 进行身份验证过程和会话管理的网络应用程序.我已经完成了后端会话的工作.现在我想在 UI 上显示登录的用户.privateContent 是一个验证是否有人登录的功能,如下所示:
I'm working on a webapp that requires atuhentication process and session management with express. I've done with the backend sessions stuff. Now i want to show on the UI the user who is signed in. privateContent is a function that verify if someone is logged in, Like so:
...
app.get( '/authRequired', queries.privateContent , routes.tasks );
...
这是查询.privateContent:
Here is queries.privateContent:
...
exports.privateContent = function ( req, res, next ) {
if ( req.session.user ) {
var username = req.session.user.username;
User.findOne( { 'username': username }, function ( err, obj ) {
if ( true ) {
next();
} else {
res.redirect('/');
}
});
} else {
res.redirect('/');
}
};
...
我想知道的是:我能像这样发送数据吗?:
What i want to know is: Am i able to send data like this? :
...
next( username );
...
如果是这样,我如何在routes.tasks呈现时检索它,如果发生如下情况(我试图在下面的代码中获取数据,但它不起作用.):
if so, how can i retrieve it when routes.tasks render, if that happens as follows (i'm trying to get the data in the code below, but it does not work.):
...
exports.my_tasks = function ( req, res, data ) {
console.log(data);
res.render('tasks/tasks',
{ title: 'Paraíso', controller: 'MyTasksController', user: data });
};
...
如您所料,我的意图是通过 next 传递登录到路由模块的当前用户,因此我可以使用 jade 在 UI 中打印用户名.谢谢您的帮助.:)
As you can guess, my intentions are to pass via next the current user who is signed in to the routing modules, so i can print the username in the UI using jade. Thank you for your help. :)
推荐答案
在这种情况下,您有几个选择(只使用其中之一!):
In this case you have a few options (only use one of these!):
- 您可以从 my_tasks 路由访问 req.session.user.username 变量
- 您可以使用res.locals
- 您可以向
req
对象添加数据
- You can just access the req.session.user.username variable from your my_tasks route
- You can use res.locals
- You can add data to the
req
object
在您的exports.privateContent 函数中,一旦在数据库中找到用户,您只需将该数据添加到res.locals:
In your exports.privateContent function, once a user is found in the database, you can simply add that data to the res.locals:
User.findOne( { 'username': username }, function ( err, obj ) {
if ( true ) {
// this variable will be available directly by the view
res.locals.user = obj;
// this will be added to the request object
req.user = obj;
next();
} else {
res.redirect('/');
}
});
然后在您的exports.my_tasks 路由中,res.locals.user
将是中间件中的任何obj.然后,您可以在视图中以变量 user
的形式简单地访问它.
Then in your exports.my_tasks route, res.locals.user
will be whatever obj was in the middleware. You can then simply access that in the view as the variable user
.
因此,总的来说,您可以通过以下方式访问路线中的数据:
So, all together, you can access the data in your route in these ways:
exports.my_tasks = function ( req, res ) {
res.render('tasks/tasks', {
userFromReq: req.user, // this exists because you added in the middleware
userFromSession: req.session.user, // this was already in the session, so you can access
userFromRes: [DO NOT NEED TO DO THIS] // because res.locals are sent straight to the view (Jade).
});
};
这篇关于我可以通过 express next() 函数发送数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!