Error: Can't set headers after they are sent.
来源:互联网 发布:陌陌协议软件 编辑:程序博客网 时间:2024/05/21 19:39
问题描述:
系统:Windows 7 x64
Node.js版本:版本是:v4.2.4 LTS
安装过程参考:《Node.js入门》Windows 7下Node.js Web开发环境搭建笔记
一段后端模拟HTTP请求的代码出现以下错误:Error: Can't set headers after they are sent.
如下图:
相关代码:
app.get('/trans', function (req, res) {var body = '';//这是需要提交的数据params = { 'source':'en', 'target':'es', 'text':'hello' }var data = qs.stringify(params);http.get("https://gateway.watsonplatform.net/language-translation/api/v2/translate?" + data, function(gres) { console.log("Got response: " + gres); gres.on('data',function(d){body += d; }).on('end', function(){ //console.log(gres.headers); console.log(body); res.send(body); });}).on('error', function(e) { console.log("Got error: " + e.message); });res.send(body);});问题原因:
出现此错误的原因是因为
http.get中的响应代码是异步的,
但在它响应之前,也就是最下面的一句“res.send(body);”已经把响应流关闭了。
解决方法:
将最下面的一句“res.send(body);”注释了就OK了。
1 0
- Error: Can't set headers after they are sent.
- node "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
- Node.js Error: 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
- 关于分布式事务、两阶段提交协议、三阶提交协议(2pc 3pc 都无法保证彻底一致性,除了Paxos 协议)
- MVC,MVP 和 MVVM 的图示
- 无聊时总结总结算法之02排序(希尔 完结)
- 三目运算符
- 安卓第十夜 亚当的诞生
- Error: Can't set headers after they are sent.
- Android之Button点击事件处理的几种方式
- hdu3183RMQ
- python:open/文件操作
- 键盘录入
- oc学习笔记之协议与分类
- CSS3 @font-face详细用法
- 添加WebStorm 项目 到 Bitbucket
- 剑指Offer:顺时针打印矩阵(JAVA)