outgoingMessage.setHeaders(headers)


返回响应对象。

为隐式标头设置多个标头值。 headers 必须是 HeadersMap 的一个实例,如果一个标头已经存在于待发送的标头中,它的值将被替换。

const headers = new Headers({ foo: 'bar' });
response.setHeaders(headers);

或者

const headers = new Map([['foo', 'bar']]);
res.setHeaders(headers);

当标头已使用 outgoingMessage.setHeaders() 设置时,则它们将与任何传给 response.writeHead() 的标头合并,其中传给 response.writeHead() 的标头优先。

// 返回 content-type = text/plain
const server = http.createServer((req, res) => {
  const headers = new Headers({ 'Content-Type': 'text/html' });
  res.setHeaders(headers);
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('ok');
});

Returns the response object.

Sets multiple header values for implicit headers. headers must be an instance of Headers or Map, if a header already exists in the to-be-sent headers, its value will be replaced.

const headers = new Headers({ foo: 'bar' });
response.setHeaders(headers);

or

const headers = new Map([['foo', 'bar']]);
res.setHeaders(headers);

When headers have been set with outgoingMessage.setHeaders(), they will be merged with any headers passed to response.writeHead(), with the headers passed to response.writeHead() given precedence.

// Returns content-type = text/plain
const server = http.createServer((req, res) => {
  const headers = new Headers({ 'Content-Type': 'text/html' });
  res.setHeaders(headers);
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('ok');
});