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
EDIT: as uʍop ǝpısdn (cool nick btw) answer, res.setHeader works. But why res.location doesn't?
The res object exposes setHeader():
res.setHeader('Location', foo);
Try that instead of res.location.
you're setting res.location. res.location is a function.
res.location('/customers/' + inst._id)