如何调试 Node.js 中的标头发送错误
我收到很多发送后无法设置标头
错误,而且他们似乎从未在我的app.js
中给我行号,这正常吗?人们如何调试这些错误?
我抛出标题错误的代码看起来像这样,它是否做了一些奇怪的事情来隐藏行号?
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');
}
});
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');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发送后无法设置标头
是一个常见错误,这意味着您基本上调用
res.render
、res.end
或res.send 多次。这意味着您尝试向一个 HTTP 请求写入多个 HTTP 响应(这是无效的)。
此错误的一个常见原因是在一个中间件中调用
next
两次。也许你有一个像这样的中间件
Can't set headers after they are sent
Is a common error which means your basically calling
res.render
,res.end
orres.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