How can I send back response headers with Node.js / Express?

Shamoon picture Shamoon · Oct 6, 2011 · Viewed 99.1k times · Source

I'm using res.send and no matter what, it returns status of 200. I want to set that status to different numbers for different responses (Error, etc)

This is using express

Answer

Bernardo Dal Corno picture Bernardo Dal Corno · Jun 24, 2016

For adding response headers before send, you can use the setHeader method:

response.setHeader('Content-Type', 'application/json')

The status only by the status method:

response.status(status_code)

Both at the same time with the writeHead method:

response.writeHead(200, {'Content-Type': 'application/json'});