可以通过express next()函数发送数据吗? [英] Can I send data via express next() function?

查看:757
本文介绍了可以通过express next()函数发送数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个需要认证过程和会话管理的webapp。我已经完成了后端会话。现在我想在UI上显示登录的用户。privateContent是一个功能,用于验证是否有人登录,如下所示:

  ... 
app.get('/ authRequired',queries.privateContent,routes.tasks);
...

这里是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('/');
}
};
...

我想知道的是:我能够发送数据这个? :

  ... 
next(username);
...

如果是这样,当routes.tasks渲染时,如何检索它,如果发生如下(我试图获取下面的代码中的数据,但它不工作。):

  ... 
exports.my_tasks = function(req,res,data){
console.log(data);
res.render('tasks / tasks',
{title:'Paraíso',controller:'MyTasksController',user:data});
};
...

你可以猜到,我的意图是通过下一个当前的登录到路由模块的用户,所以我可以使用翡翠在UI中打印用户名。
谢谢你的帮助。 :)

解决方案

在这种情况下,您有几个选项(仅使用其中的一个!):


  1. 您可以从my_tasks路由

  2. 访问req.session.user.username变量您可以使用 res.locals

  3. 您可以将数据添加到 req 对象

在exports.privateContent函数中,一旦在数据库中找到用户,您可以简单地将该数据添加到res.locals中:

  User.findOne({'username':username},function(err,obj ){
if(true){
//该变量将直接由视图
res.locals.user = obj;

//这将会被添加到请求对象
req.user = obj;

next();
} else {
res.redirect('/');
}
});

然后在exports.my_tasks路由中, res.locals.user 将是中间件中的任何obj。然后,您可以在视图中直接访问变量 user



所有这些都可以访问您的路线中的数据以这些方式:

  exports.my_tasks = function(req,res){
res。 render('tasks / tasks',{
userFromReq:req.user,//这是存在的,因为你在中间件中添加了
userFromSession:req.session.user,//这已经在会话中,所以你可以访问
userFromRes:[不需要这样做] //因为res.locals直接发送到视图(Jade)
});
};


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 );
...

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 );
...

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 });
};
...

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!):

  1. You can just access the req.session.user.username variable from your my_tasks route
  2. You can use res.locals
  3. You can add data to the req object

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('/');
  }
});

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆