发送整数值作为对 NodeJS API 调用的响应时未收到响应
no response received when sending integer value as response to NodeJS API call
我试图在调用 API 端点后发送一些整数值作为响应。
这是说明问题的示例代码:
app.post('/add5', (req, res) => {
result = req.body.num + 5;
res.send(result);
});
这是请求负载:
{
"num": 7
}
预期输出:12
但是在终端中记录了以下输出并且没有收到(空白)响应。
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 12
at new NodeError (node:internal/errors:371:5)
at ServerResponse.writeHead (node:_http_server:272:11)
at ServerResponse._implicitHeader (node:_http_server:263:8)
at ServerResponse.end (node:_http_outgoing:871:10)
at ServerResponse.send (/storage/emulated/0/codes/api/node_modules/express/lib/response.js:221:10)
at /storage/emulated/0/codes/api/index.js:208:7
at Layer.handle [as handle_request] (/storage/emulated/0/codes/api/node_modules/express/lib/router/layer.js:95:5)
at next (/storage/emulated/0/codes/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/storage/emulated/0/codes/api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/storage/emulated/0/codes/api/node_modules/express/lib/router/layer.js:95:5)
但是,在字符串中添加一些字符串完全没问题。
app.post('/add5', (req, res) => {
result = req.body.num + 5;
res.send("Result: " + result);
});
这个returns预期输出即结果:12
使用res.json(result)
或res.send(JSON.stringify(result))
或res.send(200, result)
。否则 express 假定 you're passing a http status code to send
, as the error message signifies. Or upgrade to express 4, where this cannot happen any more.
我试图在调用 API 端点后发送一些整数值作为响应。 这是说明问题的示例代码:
app.post('/add5', (req, res) => {
result = req.body.num + 5;
res.send(result);
});
这是请求负载:
{
"num": 7
}
预期输出:12
但是在终端中记录了以下输出并且没有收到(空白)响应。
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 12
at new NodeError (node:internal/errors:371:5)
at ServerResponse.writeHead (node:_http_server:272:11)
at ServerResponse._implicitHeader (node:_http_server:263:8)
at ServerResponse.end (node:_http_outgoing:871:10)
at ServerResponse.send (/storage/emulated/0/codes/api/node_modules/express/lib/response.js:221:10)
at /storage/emulated/0/codes/api/index.js:208:7
at Layer.handle [as handle_request] (/storage/emulated/0/codes/api/node_modules/express/lib/router/layer.js:95:5)
at next (/storage/emulated/0/codes/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/storage/emulated/0/codes/api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/storage/emulated/0/codes/api/node_modules/express/lib/router/layer.js:95:5)
但是,在字符串中添加一些字符串完全没问题。
app.post('/add5', (req, res) => {
result = req.body.num + 5;
res.send("Result: " + result);
});
这个returns预期输出即结果:12
使用res.json(result)
或res.send(JSON.stringify(result))
或res.send(200, result)
。否则 express 假定 you're passing a http status code to send
, as the error message signifies. Or upgrade to express 4, where this cannot happen any more.