Local Express server refuses Dgraph (docker) GET request

As the title suggests, I am unable to connect to my local express server. I am using the general cors() function to allow all routes to be ‘CORS-approved.’ The code is as follows for the express app:

// imports

app.use(cors())
app.get('/foo', (req,res) => {
    res.send({foo: "bar"})
})
app.listen(4000, () => {
    // logs
})

The schema for the remote connection is as follows:

type Fetching @remote {
  foo: String
}

type Mutation {
  Fetch: Fetching
    @custom(http: { url: "http://localhost:4000/foo", method: "GET" })
}

I am using standard ports for the Dgraph container (8000, 8080, 9080).

Edit:
I am quite unsure where the problem lies. The error message only states the following:

Evaluation of custom field failed because external request returned an error: Get "http://localhost:4000/foo\": dial tcp 127.0.0.1:4000: connect: connection refused for field: Fetch within type: Mutation.

I assume your nodejs app is on port 4000?

Thank you for replying :slight_smile: Yes, it’s on port 4000, but I’ve tried other ports as well (including 9099, 5500, 5000, and 3000).

I’ve finally resolved it. Apparently, WSL 2 doesn’t expose the network of the distro to the host computer which is why I cannot connect to the node server. What I did was to use the IPv4 address of the computer to map the custom HTTP request URL. This solution was found on:

WSL2 cannot connect to localhost when the service is running on Windows · Issue #5211 · microsoft/WSL (github.com)

Changes to the mutation became (assuming IPv4 address of the host computer is 192.168.0.1):

Fetch: Fetching
    @custom(http: { url: "http://192.168.0.1:4000/try", method: "GET" })

However, it would be much appreciated if you can provide me with some other details to mapping the WSL 2 ports onto the host computer. Thanks once again!

Edit
I also needed to expose the IPv4 of the virtual machine to the host through netsh. The solution to that can be found here:

WSL Port Forwarding - DEV Community

2 Likes

As a side note, I’ve also found another way. Instead of mapping the ports through netsh, we can use http://host.docker.internal:<port#> to connect outside of docker as documented here:

Networking features in Docker Desktop for Windows | Docker Documentation

So instead of:

We can use:

Fetch: Fetching
    @custom(http: { url: "http://host.docker.internal:4000/try", method: "GET" })