如何在NodeJS Express中发送额外的数据以及res.redirect



因为我们可以发送额外的数据或"消息";和下面例子中的res.render一样,我们能在res.redirect中做同样的事情吗?res.render("仪表板",{message:"欢迎来到仪表板页面"}(;

您可以使用会话通过重定向传递数据

闪存包-https://www.npmjs.com/package/connect-flash

app.get('/flash', function(req, res){
// Set a flash message by passing the key, followed by the value, to req.flash().
req.flash('info', 'Flash is back!')
res.redirect('/');
});

app.get('/', function(req, res){
// Get an array of flash messages by passing the key to req.flash()
res.render('index', { messages: req.flash('info') });
});

有几种方法可以在重定向中传递数据,但最正确的方法是使用query string传递数据。如前所述,你总是需要确保你的URL是正确编码的

app.get('/redirect', function(req, res) {
var string = encodeURIComponent('something that would break');
res.redirect('/your/redirection/url/?data=' + string);
});

最新更新