我是 nodejs 及其路由系统的新手。我收到“发送后无法设置标题”。仅在 Heroku 上的生产模式下(在本地工作正常)。
// 登录 ==========================================
router.get('/', ifLoggedOut, function(req, res, next){
res.render('login', { message: req.flash('message')});
});
router.post('/login', function(req, res, next){
var username = req.body.username;
var password = req.body.password;
req.checkBody('username', 'Username field is required').notEmpty();
req.checkBody('username', 'Password field is required').notEmpty();
req.checkBody('password', 'Invalid Credintials').len(8, 20);
req.checkBody('username', 'Invalid Credintials').len(4, 20);
var errors = req.validationErrors();
if(errors) {
res.render('login.ejs', {errors: errors});
} else {
passport.authenticate('local-login', {
successRedirect : '/list', // redirect to the secure list section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
})(req, res, next);
}
});
// 函数 ================================================ =
function ifLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated()) {
return next();
}
console.log("cannot found in session");
res.redirect('/');
}
function ifLoggedOut(req, res, next){
if(req.isAuthenticated()){
res.redirect('/list');
}
return next();
}
app.use('/', router);
}
// Heroku 的错误日志
2015-08-20T07:37:07.490091+00:00 app[web.1]:错误:发送后无法设置标头。2015-08-20T07:37:07.490096+00:00 应用程序 [web.1]:在 ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11) 2015-08-20T07:37:07.490098+00:00 应用程序 [ web.1]:在 ServerResponse.header (/app/node_modules/express/lib/response.js:718:10) 2015-08-20T07:37:07.490099+00:00 app[web.1]:在 ServerResponse。发送(/app/node_modules/express/lib/response.js:163:12)2015-08-20T07:37:07.490101+00:00 app[web.1]:完成(/app/node_modules/express/lib /response.js:957:10) 2015-08-20T07:37:07.490103+00:00 app[web.1]: 在 View.exports.renderFile [作为引擎] (/app/node_modules/ejs/lib/ejs .js:355:10) 2015-08-20T07:37:07.490105+00:00 app[web.1]: 在 View.render (/app/node_modules/express/lib/view.js:126:8) 2015 -08-20T07:37:07.490106+00: