如何在Node.js中调试发送错误的头文件 [英] How to debug headers sent errors in Node.js
问题描述
我收到很多发送
错误后无法设置标题,并且他们似乎从未在我的 app.js
,这是正常的吗?人们如何调试这些错误?
抛出头文件错误的代码看起来像这样,是否奇怪地隐藏了行号? b
$ b
app.get('/',function(req,res,next){
if(req.param('q')) ){
searchProvider.search(
req.param('q'),
function(error,results){
res.render('search',{
本地人:{
results:results,
q:req.param('q')
},
});
}
);
} else {
res.render('index');
}
});
无法设置标题在发送后
是一个常见错误,这意味着您的基本调用 res.render
, res.end
或 res.send
多次。这意味着你试图向一个HTTP请求写入多个HTTP响应(这是无效的)。
这个错误的一个常见原因是调用 next
在一件中间件中两次。
也许你有一块中间件,比如
app.all(*,function(req,res,next){
// not logged in
if(!req.user){
res.render(loginError);
}
//错误的下一次调用!!在调用登录错误后再调用
next();
});
I'm getting a lot of Can't set headers after they are sent
errors, and they never seem to give me line numbers in my app.js
, is this normal? How do people debug these errors?
My code that is throwing the headers error looks like this, is it doing something weird to hide the line numbers?
app.get('/', function(req, res, next) {
if (req.param('q')) {
searchProvider.search(
req.param('q'),
function( error, results) {
res.render('search', {
locals: {
results: results,
q: req.param('q')
},
});
}
);
} else {
res.render('index');
}
});
Can't set headers after they are sent
Is a common error which means your basically calling res.render
, res.end
or res.send
multiple times. This means your trying to write multiple HTTP responses to one HTTP request (this is invalid).
A common cause of this bug is calling next
twice in a piece of middleware.
Maybe you have a piece of middleware like
app.all("*", function(req, res, next) {
// not logged in
if (!req.user) {
res.render("loginError");
}
// bad accidental next call!! Will call next after rendering login error
next();
});
这篇关于如何在Node.js中调试发送错误的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!