# Nodejs fetch and Traefik

**URL:** <https://community.traefik.io/t/nodejs-fetch-and-traefik/23143>\
**Category:** Traefik v2\
**Tags:** docker\
**Created:** [June 12, 2024, 7:15am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143 "2024-06-12T07:15:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KgoEonix](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@KgoEonix](https://community.traefik.io/u/KgoEonix)\
**Post date:** [June 12, 2024, 7:15am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/1 "2024-06-12T07:15:53Z")

</div>

Hi Guys, we have a docker compose with a traefik (v2.11) as a proxy for multiple services (postgres, keycloak, api, front, ...)

I have a problem because a traefik hostname is accessible from the browser (nice) but not from a nodejs application that use `fetch`. As far as i known it's probably a DNS issue, but i can't find a way to resolve it.

```yaml
http:
  routers:
    traefik:
      rule: "Host(`traefik.docker.localhost`)"
      service: "api@internal"
      tls:
        domains:
          - main: "docker.localhost"
            sans:
              - "*.docker.localhost"

tls:
  certificates:
    - certFile: "/etc/traefik/ssl/local-cert.pem"
      keyFile: "/etc/traefik/ssl/local-key.pem"

```

```auto
global:
  sendAnonymousUsage: false

api:
  dashboard: true
  insecure: true

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    watch: true
    exposedByDefault: false

  file:
    filename: /etc/traefik/config.yml
    watch: true

log:
  level: INFO
  format: common

accessLog: {}

entryPoints:
  http:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: https
          scheme: https
  https:
    address: ":443"
  postgres:
    address: ":5432"

```

and the keycloak docker compose configuration

```auto
  keycloak:
    image: ${REGISTRY:-msdepi}/keycloak:${PLATFORM:-linux}-${TAG:-latest}
    profiles:
      - infra
    build: ./.local/.keycloak
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD}
      KC_DB: postgres
      KC_DB_URL_HOST: postgres
      KC_DB_USERNAME: postgres
      KC_DB_PASSWORD: ${POSTGRES_PASSWORD}
      KC_DB_URL_DATABASE: ${DB_NAME}
      KC_DB_SCHEMA: public
      KC_PROXY: edge
      KC_HTTP_ENABLE: true
      KC_HTTP_HOST: 0.0.0.0
      KC_HOSTNAME_STRICT: false
      KC_HOSTNAME: keycloak.docker.localhost
    command: start --import-realm
    volumes:
      - ./.local/.keycloak/realm-config/:/opt/keycloak/data/import/
    depends_on:
      postgres:
        condition: service_healthy
    labels:
      - "traefik.enable=true"
      - "traefik.http.services.keycloak.loadbalancer.server.port=8080"
      - "traefik.http.services.keycloak.entrypoints=http"
      - "traefik.http.routers.keycloak.rule=Host(`keycloak.docker.localhost`)"
      - "traefik.http.routers.keycloak.tls=true"

```

---

<div class="post-metadata">

**Author:** ![KgoEonix](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@KgoEonix](https://community.traefik.io/u/KgoEonix)\
**Post date:** [June 12, 2024, 7:25am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/2 "2024-06-12T07:25:41Z")

</div>

Whe using the fetch in node

```auto
fetch('https://keycloak.docker.localhost/')
  .then(resp => console.log(resp))
  .catch(err => console.error(err))

```

i have this error

```auto
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11457:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: getaddrinfo ENOTFOUND keycloak.docker.localhost
      at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
    errno: -3008,
    code: 'ENOTFOUND',
    syscall: 'getaddrinfo',
    hostname: 'keycloak.docker.localhost'
  }
}

```

which just says that it can't resolve the domain name

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [June 12, 2024, 7:33am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/3 "2024-06-12T07:33:43Z")

</div>

> [@KgoEonix](#):
>
> `docker.localhost, *.docker.localhost`

Did you configure those domains in your local `hosts` file or on your DSL router?

---

<div class="post-metadata">

**Author:** ![KgoEonix](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@KgoEonix](https://community.traefik.io/u/KgoEonix)\
**Post date:** [June 12, 2024, 7:41am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/4 "2024-06-12T07:41:16Z")

</div>

No, but the `keycloak.docker.localhost` is acessible from the browser without any issue but not from node. Which i need to authenticate my user from the NextJs application

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [June 12, 2024, 7:55am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/5 "2024-06-12T07:55:22Z")

</div>

Some browsers are automatically resolving any `*.localhost` to `127.0.0.1`. Node will not do that.

---

<div class="post-metadata">

**Author:** ![KgoEonix](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@KgoEonix](https://community.traefik.io/u/KgoEonix)\
**Post date:** [June 12, 2024, 9:28am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/6 "2024-06-12T09:28:06Z")

</div>

ok thanks.  
So if i have everything inside docker, will node be able to resolve the domain name through the docker dns ?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [June 12, 2024, 10:55am UTC](https://community.traefik.io/t/nodejs-fetch-and-traefik/23143/7 "2024-06-12T10:55:16Z")

</div>

Not with `localhost`, but with service/container names.
