我们可以用节点中的redirect发送数据吗?

我试图发送redirect数据为:

this.redirect("/home",{message:'hello'}); 

但是我得到了如下结果:

 undefined redirect to home 

如果有人遇到这个问题,请帮忙。

使用框架locomotive

是的,你可以使用快速闪光 :

 app.use(flash()); ... app.get('/redirect', function (req, res) { req.flash('info', 'Flash Message Added'); res.redirect('/'); }); 

你的数据可以在res.locals.messages中res.locals.messages所以在你的视图里只是在messages var中。

快闪是基于连接闪光。 它使用会话来传输消息。

如果你正在使用机车

机车build立在Express上,保留了Node期望的function和简单性。

所以:

 module.exports = function() { ... this.use(express.bodyParser()); this.use(express.session({ secret: 'keyboard cat' })); this.use(flash()); ... } 

您可以(但不需要)使用快速闪光模块进行闪光消息传送。 快速会话模块公开的req.session和res.locals对象为编写自己的Flash中间件提供了一个途径,可以更准确地满足您的需求。 这个改编自Ethan Brown的书,Web Development with Node&Express。

 app.use(function(req, res, next){ // if there's a flash message in the session request, make it available in the response res.locals.flash = req.session.flash; // then delete it delete req.session.flash; next(); }); 

在redirect到目标路由之前,使用req.session.flash设置flash消息。

 req.session.sessionFlash = { type: 'success', message: 'This is a flash message using custom middleware and express-session.' } 

使用目标路由的res.render方法中的res.locals.flash检索Flash消息。

 res.render('index', { flash: res.locals.flash }); 

有关Express 4中有关Flash消息的更多详细信息,请参阅以下“我的要点”。

https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423