0

I am writing a little javascript application to test a small HTTP server I wrote using express. The server is working fine, I can make requests to it through a browser, and I can make requests to it through code, but only once. As soon as I perform multiple requests inside a loop, it no longer works as expected.

Here is my little client code:

const http = require('http');
let alphabet = 'abcdefghijklmnopqrstuvwxyz';

let maxLength = 3;

function permutations(perm, position, string) {
    if (position == perm.length) {
        let username = perm.join('');
        // let url = `http://www.google.com`; // Added this in as a sanity test to check that it is not the server. 
        let url = `http://localhost/${string}${alphabet[i]}`;
        let response = http.get(url, response => {
            let body = '';
            response.on('data', d => body += d);
            response.on('end', () => console.log(`${string}${alphabet[i]}: ${JSON.parse(body).exists}`));
        });
    } else {
        for (let i = 0; i < string.length; i++) {
            perm[position] = string[i];
            permutations(perm, position + 1, string);
        }
    }
}

for (let length = 1; length <= maxLength; length++) {
    let perm = [];
    for (let i = 0; i < length; i++) {
        perm.push(0);
    }
    permutations(perm, 0, alphabet);
}

Basically this code generates permutations of a certain length made up of letters defined in an alphabet, and sends a request to the server. It should receive a response back inside the callback, but the callback is never called and I get the following error:

Error: connect ENOBUFS 127.0.0.1:80 - Local (undefined:undefined)
    at Object.exports._errnoException (util.js:1029:11)
    at exports._exceptionWithHostPort (util.js:1052:20)
    at connect (net.js:887:16)
    at emitLookup (net.js:1016:7)
    at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:62:16)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:10)

However, http.get does work if I put it outside of a loop, the callback gets called, and spits out the results I am looking for (but I need it in a loop).

It also doesn't work if I just try to perform an HTTP GET on a known working server like Google, so I ruled out anything wrong with the server app I wrote.

Why is http.get not performing as expected?

**Update: **

So I tried using agent: false in the options object, and that still didn't work.

Update 2:

To further illustrate the erroneous behavior of nodejs, I have pasted a new version of my server I created for the purpose of this example:

server.js:

var http = require('http');

console.log(new Date(Date.now()).toISOString() + ' ' + 'Starting server...');

http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello!\n');

    console.log(new Date(Date.now()).toISOString() + ' ' + 'Responded to request')
}).listen(8080, '127.0.0.1');

However, the server never prints out the expected text when it is responding to a request...

wheeler
  • 2,823
  • 3
  • 27
  • 43
  • Is this a duplicate of http://stackoverflow.com/questions/10603860/very-simple-node-js-client-throws-error-enobufs-after-many-http-requests? i.e., what happens if you use `agent: false` as suggested there? – apsillers Mar 02 '17 at 18:04
  • Just tried it, and I get the same results: I passed `http.get` an options object with `agent:false` defined, as well as `host` and `path`, but nothing changed. – wheeler Mar 02 '17 at 19:13
  • Did you find any solution or good answer? Please share result. thanks – Mohammad Hossein Ganjyar Sep 07 '19 at 10:21
  • I did not, and I've forgotten the context around this. – wheeler Sep 08 '19 at 21:28

0 Answers0