Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the location response HTTP header in Express framework?

How to set the response Location HTTP header in Express? I've tried this, but it's not working:

  Controller.prototype.create = function(prop, res) {
    var inst = new this.model(prop);

    inst.save(function(err) {
      if (err) {
        res.send(500, err);
      }

      res.location = '/customers/' + inst._id;
      res.send(201, null);
    });
  };

This code is persisting a new document into MongoDB, and upon competition sets the location and send a 201 response. Got this response, no Location header is set:

HTTP/1.1 201 Created
X-Powered-By: Express
Content-Length: 0
Date: Mon, 18 Feb 2013 19:08:41 GMT
Connection: keep-alive
like image 488
gremo Avatar asked Feb 18 '13 19:02

gremo


People also ask

What is RES location?

The res. location() function is used to set the response Location HTTP header to the path parameter which is being specified. Basically is it used to set the response header. It doesn't end the response, after using it you can write a response body if you want to write.

What is Location header?

The Location response header indicates the URL to redirect a page to. It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.


2 Answers

you're setting res.location. res.location is a function.

res.location('/customers/' + inst._id)
like image 164
Jonathan Ong Avatar answered Oct 18 '22 02:10

Jonathan Ong


The res object exposes setHeader():

res.setHeader('Location', foo);

Try that instead of res.location.

like image 29
slezica Avatar answered Oct 18 '22 01:10

slezica