简体   繁体   English

节点 6.9.1:如何呈现整数状态

[英]node 6.9.1: How to render an integer status

In the node 0.12 i was rendering status like this:在节点 0.12 中,我渲染状态如下:

res.send(10)

In 6.9.1 this is not allowed anymore, seems we can only render the status code in the HTTP status ranger.在 6.9.1 中,这不再被允许,似乎我们只能在 HTTP status ranger 中呈现状态代码。 So i'm looking into how to fix this without changing the web client side.因此,我正在研究如何在不更改 Web 客户端的情况下解决此问题。

jsonp seems to be my 1st choice jsonp 似乎是我的第一选择

app.get("/test", function(req, res){
    res.jsonp({ "status": "10" });
});

but it there sth.但它有…… like res.jsonp(10) ?像 res.jsonp(10) ?

Thanks !谢谢 !

res.send() is still allowed as far as I can tell.据我所知, res.send()仍然是允许的。 It will send status 200 by default.默认情况下,它将发送状态 200。

If you want to send a different status, you can either use res.sendStatus() (which will end the request), or res.status() followed by res.send() , for a custom response.如果您想发送不同的状态,您可以使用res.sendStatus() (将结束请求),或使用res.status()后跟res.send()定义响应。

You should be able to set status like this:您应该能够像这样设置状态:

res.status(200).json(json_response);

This will set the HTTP status code to 200 and json is optional you can use it if you need to send some JSON object.这会将 HTTP 状态代码设置为 200 并且 json 是可选的,如果您需要发送一些 JSON 对象,可以使用它。

This is part of Express API, and you can find some more information in their documentation .这是 Express API 的一部分,您可以在他们的文档中找到更多信息。 I assume that you are using Express 4.我假设您使用的是 Express 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM