Node: Make http.request work offline

Created on 13 Dec 2017  Ā·  23Comments  Ā·  Source: nodejs/node

Node Version: v8.9.3
Platform: Windows 8 64-bit

I am trying to make a client communicate to a server on the same device offline through http but only get this error instead:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOENT localhost:8000
    at Object._errnoException (util.js:1024:11)
    at errnoException (dns.js:55:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)

I ran the client code online and it works so I suspect that http.request does not work without internet connectivity.

All 23 comments

I suspect that http.request does not work without internet connectivity.

That is incorrect.

Without more information (like the code you are running), it is difficult to say what is causing the problem. However, judging from your error message, I'd guess that you are setting the host or hostname option to localhost:8000 rather than localhost.

(By the way, if you think it might be helpful to have others look at your code, feel free to post your code and a question as an issue in the https://github.com/nodejs/help.)

It kind of feels like something (dns?) should throw a more helpful error in this case?

It kind of feels likeĀ somethingĀ (dns?) should throw a more helpful error in this case?

Perhaps. What might the more helpful error look like? It does seem that ENOENT isn't the right error and I'd expect something more like EAI_NONAME or Node.js's non-standard ENOTFOUND.

I guess it would help to know the code that is causing the error so we can be sure of what the problem is to make sure the error message provides correct clues as to the underlying issue.

Mh, the part that I’m not sure about: Can a string like localhost:8000 ever successfully resolve? If not, something in the error message that says ā€œinvalid hostnameā€ or so would probably be great?

I tried the suspected sequence in windows, but for both:

require('http').request({host: 'localhost:8000'})

and

require('http').request({hostname: 'localhost:8000'})

I get this sequence

Error: getaddrinfo ENOTFOUND localhost:8000 localhost:8000:80
    at errnoException (dns.js:55:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26)

that is different from what was reported. So probably root cause is something else?

The code that I ran is:

http.request({
    host: 'localhost',
    port: '8000',
    path: '/',
    method: 'GET'
}, res => {
    res.setEncoding('utf8');

    res.on('data', data => {
        console.log(data);
    });
}).end()

and it does result in the error that I stated above when offline but succeeds when I connect to a wifi.

Tried in mac, and got this result, which looks correct. I don't have a windows box where I have rights to unplug net, but will see.

#node h.js 
events.js:183
      throw er; // Unhandled 'error' event
      ^

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

tried in windows too, but the result is same as mac - it works as expected. Looks like a peculiar characteristics in your system. One of the step worth trying is to isolate (scope-in / scope-out) node from the issue by trying: (i) a browser client vs node server, (ii) browser client vs. non-node server, and their complements.

by the way, I am running a node server that listens on port 8000.

entering http://localhost:8000 on my browser works fine, both online and not.

thanks. what does

require('dns').lookup('localhost', (e, a) => {console.log(a)})

output, with wifi off?

thanks.
The output is: 127.0.0.1

I tried to reproduce the error on a different device (also windows) with the same node version, then the same error is thrown with wifi off

thanks for the info. let me investigate further with exact windows and node versions

also ping @nodejs/collaborators for pointers.

Can someone try this patch and see if it makes a difference?

diff --git a/deps/uv/src/win/getaddrinfo.c b/deps/uv/src/win/getaddrinfo.c
index 282d919cf7..aad7a96452 100644
--- a/deps/uv/src/win/getaddrinfo.c
+++ b/deps/uv/src/win/getaddrinfo.c
@@ -42,6 +42,7 @@ int uv__getaddrinfo_translate_error(int sys_err) {
     case WSAHOST_NOT_FOUND:       return UV_EAI_NONAME;
     case WSATYPE_NOT_FOUND:       return UV_EAI_SERVICE;
     case WSAESOCKTNOSUPPORT:      return UV_EAI_SOCKTYPE;
+    case WSANO_DATA:              return UV_EAI_NODATA;
     default:                      return uv_translate_sys_error(sys_err);
   }
 }

@bnoordhuis - thanks for this.
I need to see 2 things:

  • a system for building this
  • a recreate where I can test this.

@zvxayr - just checking, in case - are you in a position to build this patch?

@gireeshpunathil just push a branch to your fork that is master + @bnoordhuis 's patch, and then run node-test-commit against it.

@gibfahn - node-test-commit job does not provide a build with parameter option to me. Insufficient permissions?

https://ci.nodejs.org/job/node-test-commit/14798/console
managed to trigger one, but was a total failure. Can you please help?

Parameters needed some tweaking, CI: https://ci.nodejs.org/job/node-test-commit/14804/

With @bnoordhuis patch:

events.js:111
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND localhost localhost:8000
    at errnoException (dns.js:55:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26)

It can be simplified to:

require('dns').lookup('localhost', { hints: 1024 }, (err, addr, family) => {
    console.log(err);
})

This is how lookup is called in lib/net.js:1103. This extra hints option breaks this on Windows.

This is basically duplicate of https://github.com/nodejs/node/issues/11320. That issue was closed because there was an easy userland fix.

IMHO we should remove dns.ADDRCONFIG in hints on Windows. See simillar issue in chromium https://bugs.chromium.org/p/chromium/issues/detail?id=5234

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanjunzhi picture fanjunzhi  Ā·  3Comments

mcollina picture mcollina  Ā·  3Comments

vsemozhetbyt picture vsemozhetbyt  Ā·  3Comments

stevenvachon picture stevenvachon  Ā·  3Comments

danialkhansari picture danialkhansari  Ā·  3Comments