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


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