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


Question

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

1
25
6/6/2014 3:11:42 PM

Accepted Answer

res.writeHead(200, {'Content-Type': 'text/event-stream'});

http://nodejs.org/docs/v0.4.12/api/http.html#response.writeHead

29
10/6/2011 5:40:48 PM

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'});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon