Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun does not understand "localhost" in fetch #6885

Closed
pdenapo opened this issue Nov 3, 2023 · 4 comments
Closed

Bun does not understand "localhost" in fetch #6885

pdenapo opened this issue Nov 3, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@pdenapo
Copy link

pdenapo commented Nov 3, 2023

What version of Bun is running?

1.0.8+2a405f691e80725fe0b97b93afd3b8cfed13fa5f

What platform is your computer?

Linux 6.1.0-12-amd64 x86_64 unknown

What steps can reproduce the bug?

Run the following code that creates a hello-world express sever whith and end
point and fetch it using "localhost".
(I'm using express 4.18.2)

    var express = require("express");
    
    var app = express();
    app.get("/api", function (req, res) {
      res.send("hola");
    });
    var server = app.listen(8000, "127.0.0.1");
    
    const url = "http://localhost:8000";
    
    async function make_request() {
      let response = await fetch(url + "/api");
      const text = await response.text();
      console.log(text);
      server.close();
    }
    
    make_request();

What is the expected behavior?

Under node it returns "hola".

What do you see instead?

ConnectionRefused: Unable to connect. Is the computer able to access the url?
path: "http://localhost:8000/api"

Additional information

If you change the line with the server url to

const url = "http://127.0.0.1:8000";

everything works in bun as expected!

@pdenapo pdenapo added the bug Something isn't working label Nov 3, 2023
@JeffreyATW
Copy link

This doesn't work on Node either. I get this in Node:

➜ npx ts-node test.ts
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14152:11)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: connect ECONNREFUSED ::1:8000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
    errno: -61,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 8000
  }
}

Works in both Bun and Node if you remove the "127.0.0.1" from the listen call.

@pdenapo
Copy link
Author

pdenapo commented Nov 3, 2023

Strange. Seems to be something related to the network configuration in my machine. I'm using node 20.9.0

cat /etc/networks gives

default 0.0.0.0
loopback 127.0.0.0
link-local 169.254.0.0

However, for some reason bun and node are resolving the host address in a different way!

@pdenapo
Copy link
Author

pdenapo commented Nov 3, 2023

My operative system is Debian 12

@Electroid
Copy link
Contributor

We will fix this, duplicate of #1425

@Electroid Electroid closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants