有人了解我的nodeJS应用程序中发生了什么。 当我使用ajax进行异步调用时,服务器首先响应404错误然后加载页面。该应用程序工作正常,但我有很多无聊的日志说"发送后不能设置标题。"
这是路线部分
module.exports = function(passport) {
var router = express.Router();
........
router.get('/dashboard', isLogged, dashcontroller.showdashboard);
router.get('/dashboard/general', isLogged, dashcontroller.general);
........
return router;
}
这里是控制器
exports.showdashboard = function(req, res, next) {
if (req.user.status == 'active')
res.render('dashboard', { title: 'title' , user: req.user});
else
res.redirect('/user/logout');
next();
}
exports.general = function(req, res, next) {
if (req.user.status == 'active')
res.render('general', { title: 'title' , user: req.user});
else
res.redirect('/user/logout');
next();
}
异步请求,客户端可以从/ dashboard访问此代码并驻留在public / javascripts / code.js中。客户找到它并执行它没有任何问题。
$(function() {
$("li.infos").click(function() {
$("li.basket").removeClass("active")
$("li.infos").addClass("active")
$('.main-container')
.load('/user/dashboard/general');
});
});
一切正常,但我仍然得到那些无聊的日志
GET /stylesheets/dashboard.css 304 0.555 ms - -
GET /javascripts/dashboard.js 304 0.280 ms - -
GET /javascripts/basket.js 304 0.323 ms - -
abc deserializeUser
Executing (default): SELECT `id`, `username`, `public_key`, `email`, `password`, `last_login`, `basketcount`, `status`, `activation_token`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`id` = 6;
GET /user/dashboard/general 404 35.837 ms - 2290
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/home/me/Documents/app/node_modules/express/lib/response.js:730:10)
and the trace goes on
有谁知道发生了什么? 谢谢你的建议
答案 0 :(得分:1)
Can't set headers after they are sent."
不是一个无聊的日志消息。
您在编写http响应后正在调用next()
,这就是您遇到此问题的原因。
e.g。
exports.showdashboard = function(req, res, next) {
if (req.user.status == 'active')
return res.render('dashboard', { title: 'title' , user: req.user});
else
return res.redirect('/user/logout');
}
,与exports.general
相同。