如何在Express中设置响应Location
HTTP标头?我已经试过这一点,但它不工作:如何在Express框架中设置位置响应HTTP头?
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);
});
};
此代码是坚持一个新的文档到MongoDB的,并且在比赛设置的位置和发送201
响应。得到这个响应,没有Location
头设置:
HTTP/1.1 201 Created
X-Powered-By: Express
Content-Length: 0
Date: Mon, 18 Feb 2013 19:08:41 GMT
Connection: keep-alive
编辑:为uʍopǝpısdn(酷缺口顺便说一句)答案,res.setHeader
作品。但为什么res.location
不?
它的工作原理。但为什么'res.location'(按照文档http://expressjs.com/api.html#res.location)不? – gremo 2013-02-18 19:50:26
'location'不是'property',这是一种方法。参见@Johnathan Ong的回答。 – slezica 2013-02-18 20:19:00
专门针对位置,您可以使用res.location(),正如其他评论者所说。但对于其他标题或自定义标题,它是res.set(),而不是res.setHeader()。 – 2014-04-25 18:43:10