admin管理员组文章数量:1415139
If I do this with Express it fails:
res.send(13245)
It says:
express deprecated res.send(status): Use res.sendStatus(status) instead src/x.js:38:9 (node:25549) UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 13245
It's because it consider 13245
might be a status code.
I want to return 13245
anyway, is there a way do do this?
If I do this with Express it fails:
res.send(13245)
It says:
express deprecated res.send(status): Use res.sendStatus(status) instead src/x.js:38:9 (node:25549) UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 13245
It's because it consider 13245
might be a status code.
I want to return 13245
anyway, is there a way do do this?
-
1
Did you try
res.status(200).send(13245)
? – kit Commented Oct 2, 2018 at 13:32
4 Answers
Reset to default 5You have to return a String (see http://expressjs./en/api.html#res.send):
res.send('13245')
let say you are doing a calculation and you wanna send the result.
app.post("/transaction", (req, res) => {
const a = req.body.amount;
const b = req.body.price;
const total = a*b;
res.send(total.toString());
});
If you check documentation http://expressjs./en/api.html#res.send, the value just can be a Buffer object, a String, an object, or an Array. What if you send
res.send({ value: 13245 })
and then in the other side you just need to grab the value (e.g. body.value).
You can try res.send('13245');
or you can try res.send(""+13245)
.
You are getting that error because Express assumes you are trying to send a status code. So you can send it as a string and Express will accept it or join it to a string and Express should think you are sending back just some plain number and yes always check the docs when you run into trouble: http://expressjs./en/api.html#res.send
本文标签: javascriptReturn (send) int with express jsStack Overflow
版权声明:本文标题:javascript - Return (send) int with express js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745207099a2647690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论