更新了Express.js从版本2到3,以及随后的调用app.dynamicHelpers({..})打破,因为它不再出现在V3中:
app.dynamicHelpers({
request: function(req){
return req
},
...etc.
});
Run Code Online (Sandbox Code Playgroud)
有一个迁移指南说:
app.dynamicHelpers() (使用中间件+ res.locals)但我很难过如何做到这一点.有没有更具体的例子来说明如何迁移?
相关SO帖子: nodejs express 3.0
我使用的是express 4、passport 和express-flash。当我使用罐装护照中间件功能并设置 failureFlash: true 时,一切正常。但是,当我在同一路由文件的注册函数中使用自定义回调时,它不起作用。messages.info 对象为空。
这工作正常:
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: true
}),
function(req, res){
}
);
Run Code Online (Sandbox Code Playgroud)
这导致我的 messages.info 对象为空:
router.post('/register', function(req, res, next){
passport.authenticate('local-signup', function(err, user, info){
if(err){
req.flash('info', err);
res.render('register');
}else{
res.render('profile');
}
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
我使用 jade 作为我的预处理器:
if (messages.info)
.message.info
span= messages.info
Run Code Online (Sandbox Code Playgroud)
不劳而获!
在我的Express应用程序中,我可以app.locals直接在我的模板中设置和使用它们(使用快速手柄渲染).但是,当我设置res.locals中间件时,除非我将它们传入,否则它们不可用.我做错了什么?
服务器
app.locals.foo = "foo";
app.use(function(req, res, next) {
res.locals.bar = "bar";
next();
});
Run Code Online (Sandbox Code Playgroud)
模板
{{foo}} {{bar}}
Run Code Online (Sandbox Code Playgroud)
测试1
app.get("/", function(req, res) {
app.render("template.html", {}, function(error, html) {
res.send(html);
});
});
Run Code Online (Sandbox Code Playgroud)
结果
foo
Run Code Online (Sandbox Code Playgroud)
测试2
app.get("/", function(req, res) {
app.render("template.html", {bar: res.locals.bar}, function(error, html) {
res.send(html);
});
});
Run Code Online (Sandbox Code Playgroud)
结果
foo bar
Run Code Online (Sandbox Code Playgroud)