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

8

43

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

Mahayana answered 6/10, 2011 at 14:59 Comment(0)
T
41
res.writeHead(200, {'Content-Type': 'text/event-stream'});

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

Technics answered 6/10, 2011 at 17:40 Comment(0)
T
59

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'});
Tavares answered 24/6, 2016 at 2:46 Comment(0)
T
41
res.writeHead(200, {'Content-Type': 'text/event-stream'});

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

Technics answered 6/10, 2011 at 17:40 Comment(0)
C
15

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.x
res.send( "Not found", 404 );

// Express 4.x
res.status(404).send("Not found");
Ceballos answered 6/10, 2011 at 15:10 Comment(3)
Note that now the parameters are in the reverse order: res.send(404, "Not found"); The old way still works, but I would change it to keep up with the times.Commonality
Beyond the ordering problem mentioned by Martin Adoue, This answer isn't really true anymore, if it ever was. Using res.send() sets the payload, not the status message.Poco
Now this one is deprecated, even if you use in reverse order. Although it would work you should use: `res.status(404).send("Not Found");Roid
K
9

Since the question also mentions Express you could also do it this way using middleware.

app.use(function(req, res, next) {
  res.setHeader('Content-Type', 'text/event-stream');
  next();
});
Kharkov answered 10/6, 2017 at 9:39 Comment(0)
A
7

You should use setHeader method and status method for your purpose.

SOLUTION:

app.post('/login', function(req, res) {

  // ...Check login credentials with DB here...

  if(!err) {
    var data = {
      success: true,
      message: "Login success"
    };

    // Adds header
    res.setHeader('custom_header_name', 'abcde');

    // responds with status code 200 and data
    res.status(200).json(data);
  }
});
Azide answered 29/12, 2017 at 16:9 Comment(3)
This is definitely Express code, and not plain Node.jsManille
@ZachSmith The OP asked for NodeJS/Express. So what's the issue? Just to let you know, if it's just my answer's title, you could have edited it.Azide
Sorry yes - it was just the title and I could have edited it. ApologiesManille
K
4

set statusCode var before send() call

res.statusCode = 404;
res.send();
Kacerek answered 6/6, 2014 at 15:2 Comment(0)
S
4

In the documentation of express (4.x) the res.sendStatus is used to send status code definitions. As it is mentioned here each has a specific description.

res.sendStatus(200); // equivalent to res.status(200).send('OK')
res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
Soil answered 16/6, 2015 at 13:25 Comment(0)
A
3

i use this with express

res.status(status_code).send(response_body);

and this without express (normal http server)

res.writeHead(404, {
    "Content-Type": "text/plain"
});
res.write("404 Not Found\n");
res.end();
Arnoldarnoldo answered 21/1, 2015 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.