response.setHeader(name, value)
name<string>value字符串|字符串数组
为隐式头设置单个头值。如果该头在即将发送的头中已经存在,其值将被替换。这里使用字符串数组可以发送多个具有相同名称的头。
🌐 Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here to send multiple headers with the same name.
response.setHeader('Content-Type', 'text/html; charset=utf-8'); 或者
🌐 or
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']); 尝试设置包含无效字符的头字段名称或值将导致抛出 TypeError。
🌐 Attempting to set a header field name or value that contains invalid characters
will result in a TypeError being thrown.
当使用 response.setHeader() 设置了头部时,它们将与传递给 response.writeHead() 的任何头部合并,而传递给 response.writeHead() 的头部优先。
🌐 When headers have been set with response.setHeader(), 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 = http2.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('X-Foo', 'bar');
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('ok');
});