Error : getaddrinfo ENOTFOUND in node.js

在学习《Node.js In Action》时,第三章Node programming fundamentals中有讲如何处理serial flow的方法,将例子敲到电脑上却发现会报错,例子如下:

var fs = require('fs');
var request = require('request');
var htmlparser = require('htmlparser');
var configFilename = './rss_feeds.txt';

function checkForRSSFile () {
  fs.exists(configFilename, function(exists) {
    if (!exists) {
      return next(new Error('Missing RSS file: ' + configFilename));
    }
    console.log(exists);
    next(null, configFilename);
  });
}

function readRSSFile(configFilename) {
  fs.readFile(configFilename, function(err, feedList) {
    if (err) return next(err);

    feedList = feedList
              .toString()
              .replace(/^\s+|\s+$/g, '')
              .split("\n");
    var random = Math.floor(Math.random()*feedList.length);
    console.log(feedList[random]);
    next(null, feedList[random]);
  });
}

function downloadRSSFeed(feedUrl) {
  request({uri: feedUrl}, function(err, res, body) {
    if (err) return next(err);
    if (res.statusCode != 200) {
      return next(new Error('Abnormal response status code'));
    }
    console.log(body);
    next(null, body);
  });
}



function parseRSSFeed (rss) {
  var handler = new htmlparser.RssHandler();
  var parser = new htmlparser.Parser(handler);
  parser.parseComplete(rss);

  if (!handler.dom.items.length) {
    return next(new Error('No RSS items found'));
  }
  var item = handler.dom.items.shift();
  console.log(item.title);
  console.log(item.link);
}

var tasks = [ downloadRSSFeed,
              parseRSSFeed ];

function next(err, result){
  if (err) throw err;
  var currentTask = tasks.shift();

  if(currentTask) {
    currentTask(result);
  }
}

next();



它总是会报:

Error: getaddrinfo ENOTFOUND    at errnoException (dns.js:37:11)    at Object.onanswer [as oncomplete] (dns.js:124:16)

在查阅https://github.com/mikeal/request时看到

Super simple to use

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

还是会报错, 在网上查了好久都没有找到解决方法,暂时记录在此。

你可能感兴趣的:(NodeJs)