📜  Node.js http.validateHeaderName() 方法

📅  最后修改于: 2022-05-13 01:56:46.496000             🧑  作者: Mango

Node.js http.validateHeaderName() 方法

http.validateHeaderName() (在 v14.3.0 中添加)属性是“ http ”模块的内置属性,它在调用 res.setHeader(name, value)时对提供的名称执行低级验证。传递非法值作为名称将导致抛出 TypeError ,由代码标识:“ ERR_INVALID_HTTP_TOKEN ”。

在将标头传递给HTTP请求或响应之前,不必使用此方法。 HTTP模块将自动验证此类标头。

注意:使用最新版本的 Node.js 来获取所需的输出。  

为了得到响应和正确的结果,我们需要导入' http '模块。

const http = require('http');

句法:

http.validateHeaderName(name);

参数:此属性接受如上所述和如下所述的单个参数:

  • name < String > 它接受标题的名称,不区分大小写。

返回值:它不返回任何值,而是验证标头是否可以接受。

下面的示例说明了在 Node.js 中使用http.validateHeaderName()属性。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// http.validateHeaderName() Method
 
// Importing http module
var http = require('http');
const { validateHeaderName } = require('http');
 
try {
  validateHeaderName('');
} catch (err) {
  err instanceof TypeError; // true
 
  // ERR_INVALID_HTTP_TOKEN
  console.log("Error Occured", err.code);
 
  // Header name must be a valid
  // HTTP token [""]
  console.log(err.message);
}


Javascript
// Node.js program to demonstrate the
// http.validateHeaderName() Method
 
// Importing http module
var http = require('http');
 
// Another way to import
const { validateHeaderName } = 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']);
 
  // Validating headers
  try {
    validateHeaderName('Content-Type');
    console.log("Header 'Content-Type' Validated True...")
    http.validateHeaderName('set-cookie');
    console.log("Header 'set-cookie' Validated True...")
    http.validateHeaderName('alfa-beta');
    console.log("Header 'alfa-beta' Validated True...")
    validateHeaderName('@@wdjhgw');// not valid
  } catch (err) {
    err instanceof TypeError;
    console.log("Error Occured", err.code);
   
    // Prints 'Header name must be
    // a valid HTTP token [""]'
    console.log(err.message);
  }
 
  // 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...");
});


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

node index.js

输出:

示例 2:文件名:index.js

Javascript

// Node.js program to demonstrate the
// http.validateHeaderName() Method
 
// Importing http module
var http = require('http');
 
// Another way to import
const { validateHeaderName } = 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']);
 
  // Validating headers
  try {
    validateHeaderName('Content-Type');
    console.log("Header 'Content-Type' Validated True...")
    http.validateHeaderName('set-cookie');
    console.log("Header 'set-cookie' Validated True...")
    http.validateHeaderName('alfa-beta');
    console.log("Header 'alfa-beta' Validated True...")
    validateHeaderName('@@wdjhgw');// not valid
  } catch (err) {
    err instanceof TypeError;
    console.log("Error Occured", err.code);
   
    // Prints 'Header name must be
    // a valid HTTP token [""]'
    console.log(err.message);
  }
 
  // 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...");
});

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

node index.js

输出:

现在在浏览器中运行http://localhost:3000/

输出:在浏览器中

ok

参考: https://nodejs.org/api/http.html#http_http_validateheadername_name