如何在Express框架中设置位置响应HTTP标头?

时间:2013-02-18 19:10:33

标签: node.js http-headers express

如何在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没有?

2 个答案:

答案 0 :(得分:26)

您正在设置res.locationres.location是一个函数。

res.location('/customers/' + inst._id)

答案 1 :(得分:17)

res对象公开setHeader()

res.setHeader('Location', foo);

尝试而不是res.location