Node.js 节点快速获取请求传递自定义头

Node.js 节点快速获取请求传递自定义头,node.js,ajax,express,Node.js,Ajax,Express,我正在尝试发出与此jQuery等效的get请求: $.ajax({ headers: { 'X-Auth-Token': 'YOUR_API_KEY' }, url: 'http://api.football-data.org/v2/competitions/BL1/standings', dataType: 'json', type: 'GET', }).done(function(response) { console.log(r

我正在尝试发出与此jQuery等效的get请求:

  $.ajax({
    headers: { 'X-Auth-Token': 'YOUR_API_KEY' },
    url: 'http://api.football-data.org/v2/competitions/BL1/standings',
    dataType: 'json',
    type: 'GET',
  }).done(function(response) {       
    console.log(response);
  });
然而,我还没有弄明白如何使用nodejs-express来实现它。此代码来自连接到主应用程序的api路由模块。 请求似乎起作用,收集数据,但并未结束。此外,从浏览器进行检查时,我无法在请求中看到自定义标头

   app.get('/api/:league', function(req, res, next) {

      var apiKey = process.env.API_KEY;    
      let url = 'api.football-data.org';

      var options = {
        host: url,
        method: 'GET',
        path: 'v2/competitions/BL1/standings',
        headers: {
          'X-Auth-Token': apiKey
        }
      };

      let data = "";
      var getReq = http.request(options,function(resp){

         console.log("Connected");        
         resp.on("data", chunk => {
          data += chunk;
         });

         resp.on("end", () => {
           console.log("data collected");
         });
      });

      getReq.on("error", (err) => console.log("OOPS!", err));

      getReq.end(JSON.stringify(data));

  })    

尝试使用请求承诺npm包


尝试使用请求承诺npm包


jQuery ajax函数没有
标题
选项。您可以在官方文档上阅读此功能。它们通过
beforeSend
功能方式自定义请求头:

$.ajax({
    beforeSend: function (request) {
        request.setRequestHeader("X-Auth-Token", 'YOUR_API_KEY');
    },
    url: 'http://api.football-data.org/v2/competitions/BL1/standings',
    dataType: 'json',
    type: 'GET',
}).done(function (response) {
    console.log(response);
});
使用http节点库,您可以流式处理此示例

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    // TODO: send data to client
    // res.status(200).json(JSON.stringify(body.toString()))
    console.log(body.toString());
  });
});

req.end();

jQuery ajax函数没有
标题
选项。您可以在官方文档上阅读此功能。它们通过
beforeSend
功能方式自定义请求头:

$.ajax({
    beforeSend: function (request) {
        request.setRequestHeader("X-Auth-Token", 'YOUR_API_KEY');
    },
    url: 'http://api.football-data.org/v2/competitions/BL1/standings',
    dataType: 'json',
    type: 'GET',
}).done(function (response) {
    console.log(response);
});
使用http节点库,您可以流式处理此示例

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    // TODO: send data to client
    // res.status(200).json(JSON.stringify(body.toString()))
    console.log(body.toString());
  });
});

req.end();

列出的第16个参数是headers:使用XMLHttpRequest传输与请求一起发送的附加头键/值对的对象。它确实有效,我对它进行了测试。好吧,我试过你的代码,它可以保存数据:)但将JSON输出到客户端的格式不正确。@JohnnyBizzle我的错误,只需使用
JSON.parse
而不是
JSON.stringify
列出的第16个参数是headers:一个包含额外头键/值对的对象,可以使用XMLHttpRequest传输与请求一起发送。它确实有效,我测试了它。好的,我试过你的代码,它可以保存数据:)但将JSON输出到客户端的格式不正确。@JohnnyBizzle我的错误,只需使用
JSON.parse
而不是
JSON.stringify
工作起来像个符咒:)工作起来像个符咒:)