简体   繁体   中英

Node HTTP server hangs when sending 204 - NO CONTENT response

I'm having an issue wherein sending a response with a 204 - NO CONTENT status code via my NodeJS HTTP server causes the response to hang indefinitely, timing out the initiating request.

response.statusCode = 204;
response.write(preparedBody);
response.end();

The error still occurs even if preparedBody is just the empty string.

How do I get the response to go out?

Absolutely no body can be written to the response, not even the empty string.

Generally speaking, a status code of 204 indicates that the request was successfully serviced by the server, but warranted no response body, and thus none is being provided.

NodeJS, for some reason, seems to strictly prohibit setting any content in the body of a response with a 204 status code, even if the body being set is 'nothing', per-se.

Remove the offending line:

response.write(preparedBody);

...and the server should begin working as intended again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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