outgoingMessage.getHeaders()


返回当前发出的头部的浅拷贝。由于使用的是浅拷贝,数组值可以在不额外调用各种与头部相关的 HTTP 模块方法的情况下被修改。返回对象的键是头部名称,值是相应的头部值。所有头部名称都是小写的。

【Returns a shallow copy of the current outgoing headers. Since a shallow copy is used, array values may be mutated without additional calls to various header-related HTTP module methods. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.】

outgoingMessage.getHeaders() 方法返回的对象并没有从 JavaScript 的 Object 原型继承。这意味着典型的 Object 方法,例如 obj.toString()obj.hasOwnProperty() 等,并未定义,无法使用。

【The object returned by the outgoingMessage.getHeaders() method does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work.】

outgoingMessage.setHeader('Foo', 'bar');
outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);

const headers = outgoingMessage.getHeaders();
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }