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

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


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");