【发布时间】:2022-01-04 23:17:15
【问题描述】:
我是 nodeJS 的新手,我想使用 express 向浏览器显示我的请求结果正文,但是我的代码抛出了这样的错误,我尝试使用 array.push() 但也不起作用
错误
node:_http_outgoing:576
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (C:\xampp\htdocs\#masgalih\tt\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\xampp\htdocs\#masgalih\tt\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\xampp\htdocs\#masgalih\tt\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\xampp\htdocs\#masgalih\tt\node_modules\express\lib\response.js:158:21)
at Request._callback (C:\xampp\htdocs\#masgalih\tt\index.js:12:24)
at Request.self.callback (C:\xampp\htdocs\#masgalih\tt\node_modules\request\request.js:185:22)
at Request.emit (node:events:390:28)
at Request.<anonymous> (C:\xampp\htdocs\#masgalih\tt\node_modules\request\request.js:1154:10) {
code: 'ERR_HTTP_HEADERS_SENT'
}
代码
const request = require('request')
const express = require('express')
const app = express()
app.get('/getdata', (req, res) => {
if (req.query.id !== undefined && req.query.id !== '') {
request('http://localhost/myApi/index.php?id=' + req.query.id, (err, response, body) => {
return res.send({
status: 200,
data: body
})
})
}
return res.send({
status: 400,
msg: 'Parameter invalid'
})
})
app.listen(2000)
【问题讨论】:
-
您是否缺少“其他”?
标签: javascript node.js arrays