got: got request for host in hosts file throws ENOTFOUND

Describe the bug

  • Node.js version: 12.16.2 (also tested with 14.0.0)
  • OS & version: windows 10

Actual behavior

Attempting a got request against a host which is registered in my hosts files fails with an ENOTFOUND; where doing an https.get for the same host and path does not.

This used to work with got 10, but got 10 hangs on node 12 on an ubuntu 14 machine, which happens to be my target for this project 😕

Expected behavior

Should be able to retrieve data from a server by hostname according to system hosts file

Code to reproduce

(not so much code as steps):

  • set up an http server listening on 127.0.0.1
  • create a hostfile rebind for some other hostname
  • attempt got to that server

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20

Most upvoted comments

I’ve raised https://github.com/szmarczak/cacheable-lookup/issues/24 – please let me know if you need more info (:

I originally left out code because the situation is easy enough to replicate, but specific to the host system; however, as per your request, here’s a full set of steps:

1. update \windows\system32\drivers\etc\hosts to contain:

127.0.0.1      server.dev.local

2. create a new project:

mkdir got-issue
cd got-issue
npm init -y
npm install --save got express

3. add the app.js file:

var app = require("express")();
var got = require("got");
var http = require("http");

app.get('/user', (req, res) => {
  res.send({
    id: 1,
    name: "Han",
    likes: "to shoot first"
  });
});

app.get("/fails", (req, res) => {
  got("http://server.dev.local/user")
    .then(result => res.send(result.body))
    .catch(err => {
      res.status(500);
      res.send(err);
    });
});

app.get("/by-ip", (req, res) => {
  got("http://127.0.0.1/user")
    .then(result => res.send(result.body))
    .catch(err => {
      res.status(500);
      res.send(err);
    });
});

app.get("/by-http", (req, res) => {
  http.get("http://server.dev.local/user", r => {
    let data = "";
    r.on("data", d => data += d.toString());
    r.on("end", () => res.send(data));
  });
});

var server = app.listen(80, () => {
  const address = server.address();
  console.log(`listening on ${address.address}:${address.port}`);
});

4. update package.json, adding the ‘start’ script:

{
  "name": "got-issue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "got": "^11.0.2"
  }
}

5. Run it

npm start

6. Create a batch file:

curl http://localhost/user
curl http://localhost/fails
curl http://localhost/by-ip
curl http://localhost/by-http

7. Run the batch file

C:\code\scratch\got-issue>curl http://localhost/user
{"id":1,"name":"Han","likes":"to shoot first"}

C:\code\scratch\got-issue>curl http://localhost/fails
{"name":"RequestError","code":"ENOTFOUND","timings":{"start":1587992852528,"socket":1587992852529,"lookup":1587992852534,"error":1587992852534,"abort":1587992852535,"phases":{"wait":1,"dns":5,"total":7}}}

C:\code\scratch\got-issue>curl http://localhost/by-ip
{"id":1,"name":"Han","likes":"to shoot first"}

C:\code\scratch\got-issue>curl http://localhost/by-http
{"id":1,"name":"Han","likes":"to shoot first"}