response.getHeaderNames()(在 v7.7.0 中添加)方法是“http”模塊的內置應用程序編程接口,它返回一個包含當前傳出標頭的唯一名稱的數組。所有標題名稱都是小寫的。
如果已使用response.setHeader()設置標頭,則它們將與傳遞給response.writeHead()的任何標頭合並,並且標頭傳遞給response.writeHead()優先。
為了獲得響應和正確的結果,我們需要導入‘http’模塊。
用法:
const http = require('http');
用法:
response.getHeaderNames()
參數:此屬性不接受任何參數。
返回值:它以字符串格式返回所有標題的名稱,如下麵的示例所述。
下麵的示例說明了在 Node.js 中使用 response.getHeaderNames() 方法。
範例1: 文件名:index.js
// Node.js program to demonstrate the
// response.getHeaderNames() 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('Foo', 'bar');
response.setHeader('Set-Cookie',
['foo=bar', 'bar=baz']);
const headerNames = response.getHeaderNames();
console.log(headerNames);
response.end("200, ok");
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log(
"Server is running at port 3000...");
});
輸出:
Output:In Console
>> Server is running at port 3000…
>> [ ‘alfa’, ‘foo’, ‘set-cookie’ ]
現在在瀏覽器中運行http://localhost:3000 /。
輸出:在瀏覽器中
200, ok
範例2: 文件名:index.js
// Node.js program to demonstrate the
// response.getHeaderNames() 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']);
// Getting the set Headers
const headers = response.getHeaders();
const headerNames = response.getHeaderNames();
// Printing those headers
console.log(headers);
// Printing headerNames
console.log(headerNames);
var Output = "Hello Geeksforgeeks...,"
+ " Available headerNames are:"
+ JSON.stringify(headerNames);
// 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…
>> [Object:null prototype] {
alfa:‘Beta’,
alfa1:”,
alfa2:5,
‘cookie-setup’:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ]}
>> [ ‘alfa’, ‘alfa1’, ‘alfa2’, ‘cookie-setup’ ]
現在在瀏覽器中運行http://localhost:3000 /。
輸出:在瀏覽器中
Hello Geeksforgeeks…, Available headers and trailers are:{“alfa1″:””, “cookie-setup”:[“Alfa=Beta”, “Beta=Romeo”],
“content-type”:”text/plain”, “trailer”:”Content-MD5″}ok
參考: https://nodejs.org/api/http.html#http_response_getheadernames
相關用法
- 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.getHeaderNames() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。