response.setHeader(name, value)(在 v0.4.0 中添加)方法是“http”模塊的內置應用程序編程接口,它為隱式標頭設置單個標頭值。如果此標頭已存在於 to-be-sent 標頭中,則其值將被替換。在此處使用字符串數組發送多個具有相同名稱的標頭。非字符串值將不加修改地存儲。因此, response.getHeader() 可能會返回非字符串值。但是,非字符串值將被轉換為字符串以進行網絡傳輸。
如果已使用response.setHeader()設置標頭,則它們將與傳遞給response.writeHead()的任何標頭合並,並且標頭傳遞給response.writeHead()優先。
為了獲得響應和正確的結果,我們需要導入‘http’模塊。
用法:
const http = require('http');
用法:
response.setHeader(name, value)
參數:該屬性接受上述和以下描述的單個參數:
- name <String>:它接受標題的名稱,不區分大小寫。
- value <any>:它可以接受任何值,如對象、字符串、整數、數組等。
返回值:它不返回任何值,而是按如下所述設置標頭。
下麵的示例說明了在 Node.js 中使用 response.setHeader() 屬性。
範例1: 文件名:index.js
// Node.js program to demonstrate the
// response.setHeaders() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(request, response) {
// Setting up Headers
response.setHeader('Content-Type', 'text/html');
response.setHeader('Set-Cookie', ['type=ninja',
'language=javascript']);
// Checking and printing the headers
console.log("When Header is set a string:",
response.getHeader('Content-Type'));
console.log("When Header is set an Array:",
response.getHeader('Set-Cookie'));
// Getting the set Headers
const headers = response.getHeaders();
// Printing those headers
console.log(headers);
// Prints Output on the browser in response
response.writeHead(200,
{ 'Content-Type':'text/plain' });
response.end('ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log("Server is running at port 3000...");
});
現在在瀏覽器中運行http://localhost:3000 /。
輸出:
In Console
>> Server is running at port 3000…
>> When Header is set a string:text/html
>> When Header is set an Array:[‘type=ninja’, ‘language=javascript’]
>> [Object:null prototype] { ‘content-type’:‘text/html’, ‘set-cookie’:[‘type=ninja’, ‘language=javascript’]}
範例2: 文件名:index.js
// Node.js program to demonstrate the
// response.setHeaders() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(req, response) {
// Setting up Headers
response.setHeader('Alfa', 'Beta');
response.setHeader('Alfa1', '');
response.setHeader('Alfa2', 5);
response.setHeader('Cookie-Setup',
['Alfa=Beta', 'Beta=Romeo']);
// response.setHeader('', 'Beta'); // Throws Error
// response.setHeader(); // Throws Error
// Checking and printing the headers
console.log("When Header is set an Array:",
response.getHeader('Cookie-Setup'));
console.log("When Header is set an 'Beta':",
response.getHeader('Alfa'));
console.log("When Header is set '':",
response.getHeader('Alfa1'));
console.log("When Header is set number 5:",
response.getHeader('alfa2'));
console.log("When Header is not set:",
response.getHeader('Content-Type'));
// Getting the set Headers
const headers = response.getHeaders();
// Printing those headers
console.log(headers);
var Output = "Hello Geeksforgeeks..., "
+ "Available headers are:"
+ JSON.stringify(headers);
// Prints Output on the browser in response
response.write(Output);
response.end('ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log("Server is running at port 3000...");
});
使用以下命令運行index.js文件:
node index.js
輸出:
Output: In Console
Server is running at port 3000…
When Header is set an Array:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ]
When Header is set an ‘Beta’:Beta
When Header is set ”:
When Header is set number 5:5
When Header is not set:undefined
[Object:null prototype] {
alfa:‘Beta’,
alfa1:”,
alfa2:5,
‘cookie-setup’:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ]}
現在在瀏覽器中運行http://localhost:3000 /。
Output: In Browser
Hello Geeksforgeeks…, Available headers are:{“alfa”:”Beta”, “alfa1″:””, “alfa2”:5, “cookie-setup”:[“Alfa=Beta”, “Beta=Romeo”]}ok
如果調用了 response.writeHead() 方法而該方法沒有被調用,它將直接將提供的 header 值寫入網絡通道而不進行內部緩存,並且 header 上的 response.getHeader() 不會產生預期的結果。如果需要在未來可能檢索和修改標頭的漸進式填充,請使用 response.setHeader() 而不是 response.writeHead()。
參考: https://nodejs.org/api/http.html#http_response_setheader_name_value
相關用法
- Node.js process.nextTick()用法及代碼示例
- Node.js GM solarize()用法及代碼示例
- Node.js MySQL Max()用法及代碼示例
- Lodash _.method()用法及代碼示例
- Node.js Http2ServerRequest.method用法及代碼示例
- Node.js http.IncomingMessage.method用法及代碼示例
- Collect.js toArray()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- Tensorflow.js tf.Sequential.evaluate()用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- JavaScript Math cosh()用法及代碼示例
- Node.js hmac.update()用法及代碼示例
- jQWidgets jqxFormattedInput val()用法及代碼示例
- HTML DOM isEqualNode()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- Tensorflow.js tf.Tensor.buffer()用法及代碼示例
注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js response.setHeader() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。