Node.js Error: Can't set headers after they are sent
来源:互联网 发布:ubuntu引导进入win7 编辑:程序博客网 时间:2024/06/05 00:15
The res
object in Express is a subclass of Node.js's http.ServerResponse
(read the http.js source). You are allowed to call res.setHeader(name, value)
as often as you want until you call res.writeHead(statusCode)
. After writeHead
, the headers are baked in and you can only call res.write(data)
, and finally res.end(data)
.
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written. For example, look for callbacks that are accidentally called twice, or any error that happens after the body is sent.
In your case, you called res.redirect()
, which caused the response to become Finished. Then your code threw an error (res.req
is null). and since the error happened within your actual function(req,res,next)
(not within a callback), Connect was able to catch it and then tried to send a 500 error page. But since the headers were already sent, Node.js's setHeader threw the error that you saw.
Comprehensive list of Node.js/Express response methods and when they must be called:
Response must be in Head and remains in Head:
res.writeContinue()
res.statusCode = 404
res.setHeader(name, value)
res.getHeader(name)
res.removeHeader(name)
res.header(key[, val])
(Express only)res.charset = 'utf-8'
(Express only; only affects Express-specific methods)res.contentType(type)
(Express only)
Response must be in Head and becomes Body:
res.writeHead(statusCode, [reasonPhrase], [headers])
Response can be in either Head/Body and remains in Body:
res.write(chunk, encoding='utf8')
Response can be in either Head/Body and becomes Finished:
res.end([data], [encoding])
Response can be in either Head/Body and remains in its current state:
res.addTrailers(headers)
Response must be in Head and becomes Finished:
return next([err])
(Connect/Express only)- Any exceptions within middleware
function(req, res, next)
(Connect/Express only) res.send(body|status[, headers|status[, status]])
(Express only)res.attachment(filename)
(Express only)res.sendfile(path[, options[, callback]])
(Express only)res.json(obj[, headers|status[, status]])
(Express only)res.redirect(url[, status])
(Express only)res.cookie(name, val[, options])
(Express only)res.clearCookie(name[, options])
(Express only)res.render(view[, options[, fn]])
(Express only)res.partial(view[, options])
(Express only)
- Node.js Error: Can't set headers after they are sent
- node "Can't set headers after they are sent"
- Error: Can't set headers after they are sent.
- Can't set headers after they are sent
- Express: Can’t set headers after they are sent.
- Can't set headers after they are sent
- Can't set headers after they are sent
- can't set headers after they are sent
- 【Express】解决Error: Can't set headers after they are sent.
- Error: Can't set headers after they are sent to the client
- Error: Can't set headers after they are sent错误之坑
- 解决Express 的 Error: Can't set headers after they are sent.
- Nodejs Error: Can't set headers after they are sent错误解决
- [Nodejs] 记录一个小坑:Can't set headers after they are sent
- Can't set headers after they are sent 并不一定是没有return造成的
- Cloud 9 IDE 出现“Can\'t render headers after they are sent to the client”错误的临时解决方案
- CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
- Error:headers already sent ->output buffer
- 数组
- C编写循环链表,并删除其中的元素
- 第二章 学后心得【物联网1132-11】
- springMVC配置 context:annotation-config
- 排序算法扫盲
- Node.js Error: Can't set headers after they are sent
- 谈谈java虚拟机
- 动态规划中的最优二叉搜素树的介绍与理解(包含c源代码打印相关数据:e[i][j],root[i][j],w[i][j])
- 当工程总方法数超过65535处理机制
- 径向基函数(RBF)神经网络
- Eclipse下的NDK开发环境搭建
- 几种倒叙输出字符串的方式
- 【对话硅谷顶级风投】纪源资本童士豪:“在美国创业和在中国很不一样”
- Sublime text 2 快捷键