我不太确定这里出了什么问题。它应该返回给定网页的正文;说:example.com如图所示。当从一个单独的程序调用时:
console.log(aux.requestSomething('example.com'))
在控制台中看到的结果是“未定义”,这是问题,它应该从页面返回正文。
var request = require('request');
module.exports = {
requestSomething : function(address){
var x;
var v = address;
request(v, function (error, response, body) { //Grabs data from server
if (!error && response.statusCode == 200) {
x = body;
}
if (error || response.statusCode != 200 ){
console.log('Network Error ' + response.statusCode + '. Program will exit shortly...');
}
return x;
});}}