Wednesday, 17 May 2017

Error: connect ECONNREFUSED 127.0.0.1:8081 when trying to get http satus codes

I'm trying to get the satus codes of a page hosted in localhost, but I get the error

Error: connect ECONNREFUSED 127.0.0.1:8081

with the following code:

var http = require('http');
var fs = require('fs');


const options = {
    host: '127.0.0.1',
    port: '8081',
    protocol:'http:',
    path: '/index.html',
    method: 'GET'
};


var callback = function (response) {
    var arrayData = [];
    response.on('data', function (data) {
        arrayData += data;

    });

    response.on('end', function () {
        //console.log('THE DATA IS ' + arrayData);
        console.log('STATUS IS ' + response.statusCode);

    });

}

function test() {

    var req = http.request(options, callback);
    var arrayTest = [];
    req;
    arrayTest += req;
    //console.log(req);
    //console.log('ARRAY CONTENT IS ' + arrayTest);

   req.end();

}
test();

But if change the options:

const options = {
    hostname: 'www.google.com',
    protocol: 'http:',
    method:'GET'
}

The log is:

STATUS IS 302

I've searched for many answers but I couldn't figured out a suitable one for my issue. Can someone help me? Thanks



via glassraven

No comments:

Post a Comment