node.js - 在内部发出另一个 http 请求后发送 http 响应时出错:“发送后无法设置标头”
问题描述
在我的服务器内部发出另一个 http 请求后,我在 Node.js 的 express.js 中发送 http 响应时收到以下错误。
Error: Can't set headers after they are sent.
_http_outgoing.js:491
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/home/gustavo/Programacao/node-js/Imobiliaria/node_modules/express/lib/response.js:767:10)
at ServerResponse.json (/home/gustavo/Programacao/node-js/Imobiliaria/node_modules/express/lib/response.js:264:10)
at Request._callback (/home/gustavo/Programacao/node-js/Imobiliaria/api/controllers/property.js:71:41)
at Request.self.callback (/home/gustavo/Programacao/node-js/Imobiliaria/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/home/gustavo/Programacao/node-js/Imobiliaria/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
这是我的代码:
var request = require('request');
// ...
// Making http request
request(utils.url('/api/property-photos/' + propertyId), function (error, response, body) {
// ...
res.status(200).json({}).end(); // For tests
// ...
});
我认为该请求正在向我的服务器的响应发送一些响应,但我不知道为什么以及如何。
我想先感谢谁能帮助我。;)
解决方案
请求包仅用于发出请求并获得响应。所以你不能发送res.send()
通常这个错误是在你发送 res(response) 2 次或更多次时引起的
导致相同问题的示例。
route.get('/app',(req,res)=>{
res.send()
res.send()
})
如果存在类似的内容,请检查您的代码。
推荐阅读
- java - Java RMI:未找到远程接口,抛出 java.lang.ClassNotFoundException,仅在全新启动后有效
- visualization - 图形工具中具有边缘捆绑和标签的圆形布局
- javascript - 多个自定义选择文件的问题
- c - 变量具有不完整的类型“void”
- c# - C# 中的 API 响应“不支持的媒体类型”
- sql - 如何提高 SQL 空间查询性能?
- node.js - 条纹促销代码/优惠券代码检查客户端在订阅前检查
- node.js - 部署到 heroku 和 netlify 后,快速会话不持久
- reactjs - 无法通过 npm install 安装 react-scripts
- codeigniter - 如何使用 codeigniter 在弹出模式中获取行详细信息