Tuesday 11 April 2017

How do I http.get localhost

I am trying to fetch a page from port 1717 but when my bandwidth is unavailable, the http.get on error callback logs errno ENOENT whereas when I turn my bandwidth back on, it logs errno ECONNRESET. Regardless of my computer being offline or not, the url http://localhost:1717/admin/available/ ALWAYS returns content in the browser as long as the server is up and running. I've tried using postman and I have also tried using request method of the http module instead of get. I ended request after using it but I still got the same errors.

Meanwhile I have tried getting other links besides those on localhost and it fetched them. In some other threads, I saw people suggest I use hostname 127.0.0.1 instead of localhost. That too did not work so I switched to the request module from npm then there was a slight difference in behavior. In the server.js, I have sommething like this for GET requests hitting /admin/available/

            console.log('giving you table', table)
            res.writeHead(200, {"Content-Type": "text/html"});
            res.end(table); 

However, when I use the request module, it throws the error ECONNRESET but in the CLI window where the server is running, this console.log('giving you table', table) is logged, meaning the server does see that request but somehow, the module still throws ECONNRESET and the body and response variables are undefined, claiming it cannot see it. What can I do about this? I'll be posting my code below in case I'm missing something.

var http = require('http'),
component = require('../lib/render-component'),
render = {username: '', available: '', orders: '', frequent: ''}; 

// for simplicity
var request = require('request');
request('http://localhost:1717/admin/available',  function(err, res, body) {
    console.log(err, res, body)
});

// intended use scenario
http.get({port: 1717, path: '/admin/available/', headers: {Accept: 'text/html'}}, function(res) {
        var temp = '';
        res.setEncoding('utf8');

        console.log('inside get');

        res.on('data', function (chunk) {
            temp += chunk;
        }).on('end', function() {
            render.available = temp;

            http.get('http://localhost:1717/admin/order/?page=0',  function(res) {

            var temp = ''
            res.setEncoding('utf8');

            res.on('data', function (chunk) {
                temp += chunk;
            }).on('end', function() {
                render.orders = temp;

                ordersModel.find({status: 'delivered'}, 'food', function (err, orders) {
                if (err) throw err;

                var hashMap = [], returnArr = [];

                orders.forEach(function (order) {
                    hashMap.push(order.toObject()['food'].split(","));
                })

                hashMap.reduce(function(a, b) {
                    return a.concat(b)
                }, []).forEach(function(item) {

                    if ((k = returnArr.findIndex(function(elem) {
                        return elem[0] == item;
                    })) != -1) {
                        returnArr[k][1]++;
                    }
                    else returnArr.push([item, 1]);
                })

                // filter the ones with the highest value
                hashMap = [], returnArr = returnArr.sort(function(a, b) {
                    return b[1] - a[1];
                }).slice(0, 5).forEach(function(elem) {
                    hashMap.push({name: elem[0], counter: elem[1]})
                });

                render.frequent = component("frequent", hashMap);

                console.log(render)
            }) // orders model find
        }); // get orders
    }).on('error', function(e) {
            console.log(e)
        });
    }); // available on end

}).on('error', function(e) {
            console.log('err available', e)
    }); // available on error 

Please help me. I've been stuck for three days now.



via Mmayboy

No comments:

Post a Comment