如何在Express框架中设置位置响应HTTP头?
如何在Express中设置响应Location
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 (酷尼克btw)答案, res.setHeader
作品。 但为什么res.location
不?
你正在设置res.location
。 res.location
是一个函数。
res.location('/customers/' + inst._id)
res
对象公开了setHeader()
:
res.setHeader('Location', foo);
尝试而不是res.location
。