NodeJS http.request在数据处理之前结束处理

时间:2017-11-25 00:05:17

标签: json node.js get

有人可以解释为什么在实际检索任何数据之前http.request结束函数正在运行吗?我将如何进一步调试?我应该检查http状态吗?

这适用于Google Home应用,但我删除了该代码并在本地运行同样的错误。 http.request来自老师在课堂上提供的内容。

你可以粘贴:people /?search = Luke%20Skywalker 进入http://swapi.com(SW = StarWars API)以查看预期结果。



'use strict';
/*eslint no-undef: "error"*/
/*eslint-env node*/
/*eslint-disable no-console */
let http = require('http');
let starWarsAPI = `www.swapi.co`; 


//function to get details of the Star Wars Characters
//exports.handler = function(event, context, callback) {
  //console.log("event=" + JSON.stringify(event));
  //console.log("context=" + JSON.stringify(context));
  //let characterName = event.result.parameters.StarWarsCharacter;
  let characterName = "Luke Skywalker";
  console.log("**** characterName=" + characterName); 
  
  let options = searchPeopleRequestOptions(characterName);
  console.log("options=" + JSON.stringify(options)); 
  
  makeRequest(options, function( data, error) {
    console.log(" Processing data.results"); 
    let person = data.results[0];
    if (person) {
        let height = person.height;
        let mass = person.mass;
        let response = person.name + " is " + height + " centimeters tall, weighs " + mass + " kilograms";
        console.log("**** response=" + response);         
        //callback(null, {"speech": response});
    }
    else {
        console.log ("No person found"); 
        //callback(null, {"speech": "I'm not sure that character exists!"});
    }
  });
//};

console.log("The end"); 
 
//create a function to read first and last names from the API.

function searchPeopleRequestOptions(argCharacterName) {
    var pathValue = `/api/people/?search=`+ 
          encodeURIComponent(argCharacterName); 
    return {
        host: starWarsAPI, 
        path: pathValue 
    };
}
 
function makeRequest(options, callback) {
    var responseString = ""; 
    var request = http.request(options, 
    function(response) {
        response.on('data', function(data) {
            responseString += data;
            console.log("responseString=" + responseString); 
        });
         response.on('end', function() {
            console.log("end: responseString=" + responseString); 
            // dies on next line because responseString is empty 
            var responseJSON = JSON.parse(responseString);
            callback(responseJSON, null);
        });
         response.on('error', function (error) {
            console.log('\n Error received: ' + error);
        });        
    });
    request.end();
}




这是我在运行时看到的:

E:\GitHub\NealWalters\GoogleHomeTest
λ node indexTest.js
**** characterName=Luke Skywalker
options={"host":"www.swapi.co","path":"/api/people/?search=Luke%20Skywalker"}
The end
end: responseString=
undefined:1

我不确定写出" undefined:1"线。

1 个答案:

答案 0 :(得分:1)

如果您查看服务器的响应状态代码,它将是301: Moved Permanently

响应的位置字段的值是:

  

https://swapi.co/api/people/?search=Luke%20Skywalker

而不是

  

http://swapi.co/api/people/?search=Luke%20Skywalker

我们可以看到,协议从http更改为https

问题是node.js提供的http客户端不支持永久更改URL的重定向。

因此,您可以使用https模块而不是http(只需更改require('https'))。

或者使用支持重定向的包。例如axiosrequest