简体   繁体   English

如果我只想在NodeJS响应中设置状态和标头,是否需要调用'send('')'

[英]If I only want to set status and header in NodeJS response, do I need to call 'send('')'

return res
    .status(404)
    .set('Content-Type', 'text/plain');

I am only interested in sending status code and header. 我只对发送状态代码和标头感兴趣。 I have no body or message. 我没有任何讯息。 Is the above code valid, or I need to call send('') to complete construction the response object ? 上面的代码是否有效,或者我需要调用send('')完成响应对象的构造?

You can use sendStatus(code) which will just send the status code without any attached json with response 您可以使用sendStatus(code),它只会发送状态代码,而不会附带任何带有响应的json

https://expressjs.com/en/4x/api.html#res.sendStatus https://expressjs.com/en/4x/api.html#res.sendStatus

res.sendStatus(404);

or 要么

res.status(404).end();

http://expressjs.com/en/4x/api.html#res.end http://expressjs.com/en/4x/api.html#res.end

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

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