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


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


response.removeHeader()(在 v0.9.3 中添加)屬性是 ‘http’ 模塊的內置屬性,它刪除了一個由排隊等待隱式發送的名稱標識的標頭。標頭名稱匹配不區分大小寫。 response.getHeaders() 方法返回的對象在原型上並非繼承自 JavaScript 對象。

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

const http = require('http');

用法:

response.removeHeader(name);

參數:該屬性接受上述和以下描述的單個參數:

  • name <String>:它接受標題的名稱,並且不區分大小寫。

返回值:它不返回任何值,而是刪除排隊等待隱式發送的標頭。

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

範例1: 文件名:index.js

Javascript


// Node.js program to demonstrate the
// response.removeHeader() 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('Cookie-Setup',
         ['Alfa=Beta', 'Beta=Romeo']);
  console.log("Before removing headers...")
   
  // Checking and  printing the headers
  console.log("When Header is set an Array:",
  response.hasHeader('Cookie-Setup'));
  // Removing Headers
  response.removeHeader('Cookie-Setup');
  console.log("After removing headers...")
   
  // Checking and  printing the headers
  console.log("When Header is set an Array:",
  response.hasHeader('Cookie-Setup'));
  // Printing those headers
  console.log(response.getHeaders());
  response.end();
});
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});

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

輸出:

Output: (In Console)

>> Server is running at port 3000…

>> Before removing headers…

   When Header is set an Array:true

>> After removing headers…

   When Header is set an Array:false

>> [Object:null prototype] { alfa:‘Beta’}

範例2: 文件名:index.js

Javascript


// Node.js program to demonstrate the
// response.removeHeader() 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']);
  console.log("Before removing headers...")
   
  // Checking and  printing the headers
  console.log("When Header is set an Array:",
  response.hasHeader('Cookie-Setup'));
  console.log("When Header is set an 'Beta':",
  response.hasHeader('Alfa'));
  console.log("When Header is set '':",
  response.hasHeader('Alfa1'));
  console.log("When Header is set number 5:",
  response.hasHeader('alfa2'));
  console.log("When Header is not set:",
  response.hasHeader('Content-Type'));
  // Removing Headers
  response.removeHeader('Cookie-Setup');
  response.removeHeader('ALFA2');
  console.log("After removing headers...")
   
  // Checking and  printing the headers
  console.log("When Header is set an Array:",
  response.hasHeader('Cookie-Setup'));
  console.log("When Header is set an 'Beta':",
  response.hasHeader('Alfa'));
  console.log("When Header is set '':",
  response.hasHeader('Alfa1'));
  console.log("When Header is set number 5:",
  response.hasHeader('alfa2'));
  console.log("When Header is not set:",
  response.hasHeader('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 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 is running at port 3000…

>> Before removing headers…

    When Header is set an Array:true

    When Header is set an ‘Beta’:true

    When Header is set ”:true

    When Header is set number 5:true

    When Header is not set:false

>> After removing headers…

    When Header is set an Array:false

    When Header is set an ‘Beta’:true

    When Header is set ”:true

    When Header is set number 5:false

    When Header is not set:false

>> [Object:null prototype] { alfa:‘Beta’, alfa1:” }

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

Output: (In Browser)

Hello GeeksforGeeks…, Available headers are:{“alfa”:”Beta”, “alfa1″:””}

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


相關用法


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