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
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.
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.
you're setting res.location
. res.location
is a function.
res.location('/customers/' + inst._id)
The res
object exposes setHeader()
:
res.setHeader('Location', foo);
Try that instead of res.location
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With