typescriptfrontendsveltesveltekitweb-frontend

Sveltekit +page.server.ts cannot reach backend on same machine


I have a backend server in GO running on localhost:8081 exposing a couple of endpoints. These endpoints are nicely reachable with requests via CURL, Postman, ...

I am trying to build a SvelteKit frontend application running on dev default port localhost:5137. In the +page.server.ts file I have a load function trying to contact one of the endpoints on localhost:8081, but it does not work. I only ever get this error:

TypeError: fetch failed
    ...
  cause: Error: connect ECONNREFUSED 127.0.0.1:8081
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
    errno: -4078,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 8081
  }

The contents of my +page.server.svelte - load function looks like this:

export async function load({fetch}) {
    const res = await fetch('http://localhost:8081/api/...');
    return await res.json();
}

Even checked and added the missing origin headers inside the hooks.server.svelte file:

export const handleFetch: HandleFetch = async({request, fetch, event}) => {
    request.headers.set('Origin', event.url.origin);
    return fetch(request)
};

But I cannot seem to get rid of this error. Other endpoints from external public apis work nicely and I can retrieve data from them.

If anyone has any ideas, those would be greatly appreciated. I am all googled out...


Here is the list of used dependencies:

I was expecting the endpoint to be reachable like any other external public API and return the expected data so I also:


Solution

  • I finally managed to get it to work, with @carloV2 pointing me in the right direction.

    I still don't understand what the problem was, but the solution was updating node to the latest version and re-installing the node_modules. After that everything worked as expected.