How can I send back response headers with Node.js / Express?
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'});
res.writeHead(200, {'Content-Type': 'text/event-stream'});
http://nodejs.org/docs/v0.4.12/api/http.html#response.writeHead
I'll assume that you're using a library like "Express", since nodejs doesn't provide ares.send
method.
As in the Express guide, you can pass in a second optional argument to send the response status, such as:
// Express 3.xres.send( "Not found", 404 );// Express 4.xres.status(404).send("Not found");