本文介绍了如何检查Node.js中的标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可以正常工作的实际代码,但我想检查一下我的标头是否正确传输到了我的api:

Here is actual code that works well but i would like to check if my headers are well transmitted to my api:

var request = require('request');
var express = require('express');
var router = express.Router();

/* GET data by sportId */
router.get('/:locale/:sportId/:federationId/:date', function(req, res) {
    var date = req.params.date;
    var locale = req.params.locale;
    var sportId = req.params.sportId;
    var federationId = req.params.federationId;

   request(getEventsOptions(sportId, federationId, date, locale), function(error, response, body) {
    res.send(body);
   });
});

// get options for request
function getEventsOptions(sportId, federationId, date, locale)
{
    return {
        url: `http://myapi.com/event/sport/${sportId}/date-from/${date}`,
       headers: {
         'accept': 'application/json',
         'dateTo': date,
         'federationIds': federationId,
         'X-Application-ID': 'sporter',
         'Accept-Language': locale,
     }
   };
}

所以我的问题很笼统,如何在Node js应用程序中检查呼叫的标题?

So my question is quite general, how can i check headers of my call in a node js app ?

推荐答案

有三种方法可以做到这一点:

There are three ways to do this:

首先,使用req.get函数:

First, using req.get function:

req.get('headerName');

第二,使用req.header函数:

Second, using req.header function:

req.header('headerName');

第三,使用req.headers实际对象:

Third, using req.headers actual object:

req.headers['headerName'];

希望对您有帮助.

这篇关于如何检查Node.js中的标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:41