当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。