Repository navigation
Make http.request work offline #17641
Description
Activity
- changed the title
[-]Make http.get work offline[/-][+]Make http.request work offline[/+]on Dec 13, 2017 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 thehostorhostnameoption tolocalhost:8000rather thanlocalhost.(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
ENOENTisn't the right error and I'd expect something more likeEAI_NONAMEor Node.js's non-standardENOTFOUND.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:8000ever 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.
Reacted by Benjamin Gruenbaumby the way, I am running a node server that listens on port 8000.
entering
http://localhost:8000on my browser works fine, both online and not.thanks. what does
require('dns').lookup('localhost', (e, a) => {console.log(a)})
output, with wifi off?
Reacted by Benjamin Gruenbaumthanks.
The output is:127.0.0.1I 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.
9 remaining items
- added a commit that references this issue
on Dec 13, 2017 - added 2 commits that reference this issue
on Jan 8, 2018 - added 2 commits that reference this issue
on Jan 23, 2018 - added a commit that references this issue
on Jul 27, 2026
Node Version:
v8.9.3Platform:
Windows 8 64-bitI am trying to make a client communicate to a server on the same device offline through http but only get this error instead:
I ran the client code online and it works so I suspect that
http.requestdoes not work without internet connectivity.