node.js 如何在 Express 框架中设置位置响应 HTTP 标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14943607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the location response HTTP header in Express framework?
提问by gremo
How to set the response LocationHTTP header in Express? I've tried this, but it's not working:
如何Location在 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);
});
};
This code is persisting a new document into MongoDB, and upon competition sets the location and send a 201response. Got this response, no Locationheader is set:
这段代码将一个新文档持久化到 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
EDIT: as u?op ?p?sdn(cool nick btw) answer, res.setHeaderworks. But why res.locationdoesn't?
编辑:作为u?op ?p?sdn(cool nick btw) 回答,res.setHeader有效。但为什么res.location不呢?
回答by Jonathan Ong
you're setting res.location. res.locationis a function.
你正在设置res.location。res.location是一个函数。
res.location('/customers/' + inst._id)
回答by slezica
The resobject exposes setHeader():
该res对象公开setHeader():
res.setHeader('Location', foo);
Try that instead of res.location.
试试那个而不是res.location.

