1

I am trying to connect to a remote server but every time i try to create a connection i get Socket error "code":"ECONNREFUSED" , "errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":"80"} My code is as follows:

//create the connection
const client = net.createConnection({
    host: urlObj.host,
    port: urlObj.port
});


//on receiving the data, print it
client.on('data', function (data) {
    if (verbose) {
        console.log(data.toString());
    } else {
        //get rid of the head of the response if not verbose mode
        var responseSplit = data.toString().split("\r\n");
        var inBody = false;
        for (var i in responseSplit) {
            if (responseSplit[i] == "") {
                inBody = true;
            }
            if (inBody) {
                console.log(responseSplit[i]);
            }
        }
    }
});

//send the request
client.on('connect', () => {
    client.write(clientWritingString);
});

client.on('error', err => {
    console.log('socket error %j', err);
    process.exit(0);
});

And I am parsing the Url here:

//parse the URL

var urlToParse = process.argv[process.argv.length - 1];
var urlObj = url.parse(urlToParse);
if(urlObj.port==null){
    urlObj.port=80;
} else {
    urlObj.host = urlObj.host.split(":")[0];
}
O. Jones
  • 103,626
  • 17
  • 118
  • 172
BleachedAxe
  • 393
  • 2
  • 5
  • 20

1 Answers1

0

The error message shows that your program is trying to access your own machine (= 127.0.0.1). Check the original URL you are feeding to your routine and make sure the parser ends up with actual remote URL.

AnttiM
  • 1
  • 1