找不到原因 Error: Can't set headers after they are sent.
发布于 2 个月前 作者 xhuiinit 317 次浏览 来自 问答

后台一直报这个错:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\Cityb
aoCMS\node_modules\express\lib\response.js:700:10)
    at ServerResponse.res.contentType.res.type (D:\WORKING\WORKSPACE\MAVEN_PROJE
CTS\platform\CitybaoCMS\node_modules\express\lib\response.js:537:15)
    at ServerResponse.send (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\Citybao
CMS\node_modules\express\lib\response.js:129:14)
    at ServerResponse.res.send (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\Cit
ybaoCMS\node_modules\express-promise\lib\express_promise.js:200:13)
    at fn (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\CitybaoCMS\node_modules\
express\lib\response.js:934:10)
    at View.exports.renderFile [as engine] (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\
platform\CitybaoCMS\node_modules\ejs\lib\ejs.js:355:10)
    at View.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\CitybaoCMS\node
_modules\express\lib\view.js:93:8)
    at EventEmitter.app.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\Cit
ybaoCMS\node_modules\express\lib\application.js:566:10)
    at ServerResponse.res.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\C
itybaoCMS\node_modules\express\lib\response.js:938:7)
    at D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\CitybaoCMS\node_modules\expr
ess-partials\index.js:75:13
    at View.exports.renderFile [as engine] (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\
platform\CitybaoCMS\node_modules\ejs\lib\ejs.js:355:10)
    at View.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\CitybaoCMS\node
_modules\express\lib\view.js:93:8)
    at EventEmitter.app.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\Cit
ybaoCMS\node_modules\express\lib\application.js:566:10)
    at ServerResponse.res.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\C
itybaoCMS\node_modules\express\lib\response.js:938:7)
    at ServerResponse.res.render (D:\WORKING\WORKSPACE\MAVEN_PROJECTS\platform\C
itybaoCMS\node_modules\express-partials\index.js:58:9)

定位不到问题出在哪里,之前还没有问题的,什么都不处理只要请求后台就会报错。 nodejs调试起来还真实麻烦,各位大神有没有好的办法定位问题

7 回复

先设置头,再response, 明显你在代码上的逻辑有问题

碰到过类似的问题,你的 res 一定要 return.

检查一下代码是不是res了两次?

一般是判断条件触发了里面的res但是没有正确中止代码的执行,导致又触发了之后的res,所以需要在提前中断的res处要加上return

不能在writeHead 、write、end方法调用之后调用setHeader方法

callback()了多次吧

这个问题伴随我成长,一般都是因为res.send()了2次导致的, 碰到过类似的问题,你的 res 一定要 return. 这个说的对

回到顶部