Node.js response.status and response.writeHead

The statusCode property can be set using response.status and response.writeHead. The difference between them lies in the time that is whether the headers are send immediately or send later. The response.status() is called and res.statusCode is set to state the property for later use. The res.writeHead() sets the res.statusCode property and it will send headers immediately. The statusCode property value is used whenever the response sends headers. The response.sendstatus() will set the res.statusCode property and will then cause the response to be sent immediately with headers but no body. You can use respond.status() as given below.

res.status = function status(code) {

this.statusCode = code;

return this;


In the case of response.writeHead() the response headers are sent immediately so you cannot assign it afterwards.