connection refused with `load` function in sveltekit ssr

Viewed 308

I'm trying to use fetch from a load function in sveltekit with ssr.

For some reason I get connection refused:

<script context="module">
    import Jobs from '$api/jobs';

    export async function load({ fetch }) {
        const jobs = await new Jobs(fetch).getAll();

        console.log(jobs);
        return {
            props: {
                jobs
            }
        };
    }
</script>

jobs.js

import Api from './create-api';

class Jobs extends Api {
    constructor(fetch) {
        super(fetch);
    }

    async getAll() {
        return await this.api('/jobs');
    }

    async getByTag(tag) {
        return await this.api('/jobs/tags/' + tag);
    }

    async getByUsername(username) {
        return await this.api('/jobs/users/' + username.toLowerCase());
    }

    async getAllMine() {
        return await this.api('/jobs/me');
    }

    async create(job) {
        return await this.api('/jobs', job, { method: 'POST' });
    }

    async update(job) {
        return await this.api('/jobs/' + job.id, job, { method: 'PUT' });
    }

    async deleteById(id) {
        return await this.api('/jobs/' + id, {}, { method: 'DELETE' });
    }

    async getById(id) {
        console.log(id);
        return await this.api('/jobs/' + id, {}, { method: 'GET' });
    }
}

export default Jobs;

create-api.js

import { browser } from '$app/env';

class Api {
    constructor(fetch) {
        this.fetch = fetch;
    }

    async api(path, body = {}, opts = {}) {
        path = import.meta.env.VITE_API_ENDPOINT + path;
        body = JSON.stringify(body);
        const method = opts.method || 'GET';
        const headers = {};

        if (browser) {
            const token = localStorage.getItem('token');
            headers.Authorization = token ? 'Bearer ' + token : '';
        }

        const res = await this.fetch(path, {
            method: opts.method || 'GET',
            body: method === 'GET' ? null : body,
            headers
        });

        if (res.ok) {
            return await (opts.raw ? res.text() : res.json());
        }

        throw res;
    }
}

export default Api;

Here is the error:

7:54:21 PM [vite] page reload src/routes/jobs/index.svelte
request to http://localhost:3001/api/1/jobs failed, reason: connect ECONNREFUSED ::1:3001
FetchError: request to http://localhost:3001/api/1/jobs failed, reason: connect ECONNREFUSED ::1:3001
    at ClientRequest.<anonymous> (file:///home/ettinger/www/grazily.com/grazily-ui/node_modules/@sveltejs/kit/dist/install-fetch.js:5779:11)
    at ClientRequest.emit (node:events:526:28)
    at ClientRequest.emit (node:domain:475:12)
    at Socket.socketErrorListener (node:_http_client:442:9)
    at Socket.emit (node:events:526:28)
    at Socket.emit (node:domain:475:12)
    at emitErrorNT (node:internal/streams/destroy:164:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

The api works fine from onMount just not in load function.

0 Answers
Related