當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js response.hasHeader()用法及代碼示例


response.hasHeader()(在 v7.7.0 中添加)屬性是 ‘http’ 模塊的內置屬性,如果當前在傳出標頭中設置了由名稱標識的標頭,則返回 true。標頭名稱匹配不區分大小寫。 response.getHeaders() 方法返回的對象在原型上並非繼承自 JavaScript 對象。

為了獲得響應和正確的結果,我們需要導入‘http’模塊。

const http = require('http');

用法:

response.hasHeader(name);

參數:此屬性接受如上所述和以下所述的一個參數:

  • name:它是要檢查是否存在的標頭值的名稱。

返回值<Boolean>:如果當前設置了標頭的名稱,則返回 True,否則返回 False。

下麵的示例說明了在 Node.js 中使用 response.hasHeader() 屬性。

範例1: 文件名:index.js

Javascript


// Node.js program to demonstrate the
// response.hasHeaders() 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('', 'Beta'); // Throws Error
    // response.setHeader(); // Throws Error
    response.setHeader('Alfa1', '');
    response.setHeader('Alfa2', 5);
    response.setHeader('Cookie-Setup',
            ['Alfa=Beta', 'Beta=Romeo']);
    // Checking the headers
    const hasHeardere = response.hasHeader('Cookie-Setup');
    const hasHearder0 = response.hasHeader('Alfa');
    const hasHearder1 = response.hasHeader('Alfa1');
    const hasHearder2 = response.hasHeader('Alfa2');
    const hasHearder3 = response.hasHeader('Content-Type');
    // Printing output
    console.log("When Header is set an Array:", hasHeardere);
    console.log("When Header is set Beta:", hasHearder0);
    console.log("When Header is set '':", hasHearder1);
    console.log("When Header is set number 5:", hasHearder2);
    console.log("When Header is not set:", hasHearder3);
    // Getting the set Headers
    const headers = response.getHeaders();
    // Printing those headers
    console.log(headers);
    var Output = "Hello Geeksforgeeks..., " +
        "When Header is set 'Beta':" + hasHeardere +
        ", When Header is set '':" + hasHearder1 +
        ", When Header is set number 5:" + hasHearder2 +
        ", When Header is not set:" + hasHearder3;
    // Prints Hello Geeksforgeeks...
    // on browser in response
    response.write(Output);
    response.end();
});
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});

使用以下命令運行index.js文件:

node index.js

輸出:

Output:In Console…

>> server started at 3000…

When Header is set an Array:true

When Header is set ‘Beta’:true

When Header is set ”:true

When Header is set number 5:true

When Header is not set:false

>> [Object:null prototype] {

alfa:‘Beta’,

alfa1:”,

alfa2:5,

‘cookie-setup’:[‘Alfa=Beta’, ‘Beta=Romeo’]}

//現在在瀏覽器中運行 http://localhost:3000/。

Output: In Browser…

Hello Geeksforgeeks…, When Header is set ‘Beta’:true, When Header is set ”:true, When Header is set number 5:true,  

When Header is not set:false

參考: https://nodejs.org/api/http.html#http_response_hasheader_name


相關用法


注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js response.hasHeader() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。