如何在Express中设置响应Location HTTP header ?我已经尝试过了,但是没有用:

  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 header :

HTTP/1.1 201 Created
X-Powered-By: Express
Content-Length: 0
Date: Mon, 18 Feb 2013 19:08:41 GMT
Connection: keep-alive

编辑:作为的答案(很酷的昵称),res.setHeader可以工作。但是为什么 res.location 没有呢?

最佳答案

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

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

关于node.js - 如何在Express框架中设置位置响应HTTP header ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14943607/

10-16 21:24