在我的服务器中发出另一个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
// ...
});
我认为该请求正在向我的服务器的‘se响应发送一些响应,但我不知道为什么以及如何发送。
我想对能帮助我的人表示感谢。;)
发布于 2018-08-16 17:24:10
请求包仅用于发出请求并获取响应,您不能发送res.send()
,通常当您发送res(response) 2次或更多次时会导致此错误
示例来引起同样的问题。
route.get('/app',(req,res)=>{
res.send()
res.send()
})
如果存在这样的代码,请检查您的代码。
发布于 2018-08-16 19:07:16
谢谢你帮我:D
我刚找到问题所在。我在服务器当前请求中发出的http请求是异步执行的。因此,在调用它之后,在向服务器请求发送响应之前,我调用了res.end()
。我在其他http请求结果中发送的这个响应。
https://stackoverflow.com/questions/51882140
复制相似问题