response.setHeader(name, value)
添加於:v8.4.0
參數
name
<string>value
<string> | <string[]>
為隱式標頭設置單個標頭值。如果此標頭已存在於to-be-sent 標頭中,則其值將被替換。在此處使用字符串數組發送多個具有相同名稱的標頭。
response.setHeader('Content-Type', 'text/html; charset=utf-8');
或者
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
嘗試設置包含無效字符的標頭字段名稱或值將導致引發
。TypeError
當標頭已設置為
時,它們將與傳遞給 response.setHeader()
的任何標頭合並,並且傳遞給 response.writeHead()
的標頭優先。response.writeHead()
// 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');
});
相關用法
- Node.js http.ServerResponse response.setHeader(name, value)用法及代碼示例
- Node.js response.setHeader()用法及代碼示例
- Node.js http.ServerResponse response.statusCode用法及代碼示例
- Node.js http.ServerResponse response.socket用法及代碼示例
- Node.js http.ServerResponse response.statusMessage用法及代碼示例
- Node.js http2.Http2ServerResponse response.socket用法及代碼示例
- Node.js http2.Http2ServerResponse response.statusCode用法及代碼示例
- Node.js response.writeContinue()用法及代碼示例
- Node.js http2.Http2ServerResponse response.removeHeader(name)用法及代碼示例
- Node.js response.removeHeader()用法及代碼示例
- Node.js http.ServerResponse response.getHeaderNames()用法及代碼示例
- Node.js http2.Http2ServerResponse response.hasHeader(name)用法及代碼示例
- Node.js http.ServerResponse response.removeHeader(name)用法及代碼示例
- Node.js http.ServerResponse response.hasHeader(name)用法及代碼示例
- Node.js http.ServerResponse response.getHeader(name)用法及代碼示例
- Node.js http.ServerResponse response.getHeaders()用法及代碼示例
- Node.js response.hasHeader()用法及代碼示例
- Node.js response.getHeaders()用法及代碼示例
- Node.js http2.Http2ServerResponse response.getHeaders()用法及代碼示例
- Node.js response.writeHead()用法及代碼示例
- Node.js http2.Http2ServerResponse response.writeHead(statusCode[, statusMessage][, headers])用法及代碼示例
- Node.js response.addTrailers()用法及代碼示例
- Node.js http2.Http2ServerResponse response.getHeader(name)用法及代碼示例
- Node.js response.getHeaderNames()用法及代碼示例
- Node.js response.write()用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 response.setHeader(name, value)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。