0

I am trying to run a nodejs program and getting the error:

 if (err) throw err;
           ^

Error: connect ECONNREFUSED 54.144.222.65:80
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

Now, I googled and searched Stack Overflow and there are several questions related to this. But don't mark this question as "have been solved already", because all of these questions have answers that don't solve the problem. (For example some require some connection to a local server that is running but is not and the answe is "run the server" but this is not the case for my problem)

The closest I supect is to my problem is the accepted answer to this question. I also am behind a corporate proxy. But I did what the answer suggested and my proxys are settled to the correct proxy.

I also did npm config list and I got

; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/5.5.1 node/v8.9.1 win32 x64"

; userconfig C:\Users\me\.npmrc
http-proxy = "http://192.168.1.252:8080"
https-proxy = "http://192.168.1.252:8080/"
proxy = "http://192.168.1.252:8080/"
registry = "https://registry.npmjs.org/"
strict-ssl = false

; builtin config undefined
prefix = "C:\\Users\\me\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = D:\me\path\to\code_Node.js\code\3\random_story
; HOME = C:\Users\me
; "npm config ls -l" to show all defaults.

The Problem

I am trying to run this code

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

function checkForRSSFile () {
  fs.exists(configFilename, function(exists) {    //fs exists has been deprecated  udr fs.stat() or fs.acess() instead
    if (!exists)
      return next(new Error('Missing RSS file: ' + configFilename));

    console.log("Next for "+configFilename);
    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("Next for feedList[random] "+ feedList[random]);
    next(null, feedList[random]);
  });
}

function downloadRSSFeed (feedUrl) {
  request({uri: feedUrl}, function(err, res, body) {
    if (err)  throw err;//return next(err);  //<=====HERE ! THE ERROR IS HERE!!
    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 = [ checkForRSSFile,
              readRSSFile,
              downloadRSSFeed,
              parseRSSFeed ];
*/

var tasks= [ checkForRSSFile,
             readRSSFile,
             downloadRSSFeed];
function next(err, result) {
  if (err) throw err;

  var currentTask = tasks.shift();

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

next();

As you can see the error happened in downloadRSSFeed when an error is trying to be thrown. rss_feed.txt has some rss adresses like http://dave.smallpict.com/rss.xml

EDIT: I actually found the "solution" to this problem in this link. (Well solution is a stretch. It solved this problem but I have not been able to connect to the sites yet but I suppose that is not a problem of the program I guess)

The solution is to put proxy info in request as this

function downloadRSSFeed (feedUrl) {
  request({uri: feedUrl,
           'proxy':'http://192.168.1.252:8080'

           },
           function(err, res, body) {
    if (err)  throw err;//return next(err);  //<=====HERE ! THE ERROR IS HERE!!
    if (res.statusCode != 200)
      return next(new Error('Abnormal response status code'))
    console.log(body);
    next(null, body);
  });
}

As I said it is not working yet, I got a Abnormal response status code error but at least it seems one step further

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • The error `ECONNREFUSED` is exactly what it sounds like: The connection was refused by the system you're attempting to connect to. Is there something listening on the remote system at the specified address and port? – Some programmer dude Nov 12 '18 at 09:41
  • this error happens with any remote system I believe. The remote system is the url I posted so it is external – KansaiRobot Nov 12 '18 at 09:41
  • Any? So https://stackoverflow.com/feeds gives the same error when using a valid DNS resolvable name and valid URL? If so, then you have connectivity issues and that's not a "programming" issue to be resolved here. If you can reach that URL from your program then it's an "internal" connectivity issue with your other sources. – Neil Lunn Nov 12 '18 at 09:46
  • I actually found what was the root of the problem. I will write it in a Edit of this question. However now I have a different problem :( – KansaiRobot Nov 12 '18 at 09:50

1 Answers1

0

The error ECONNREFUSED is exactly what it sounds like: The connection was refused by the system you're attempting to connect why because port number 80 which is already used in another application. Change the port number and try to run application.