Auryn
Auryn

Reputation: 1176

How to proxy on Svelte-kit in dev mode

I am trying to redirect for local development my requests to /api/** to my backend server.

So a request to http://localhost:3000/api/upload goes to http://localhost:8080/api/upload.

I cannot find any svelte.config.js configuration, to get this to work for dev. Also svelte-kit dev does not provide this configuration (or I cannot find it).

Does anyone know how to do so in svelte-kit?

Upvotes: 9

Views: 11696

Answers (2)

Coreus
Coreus

Reputation: 5610

Here's the Typescript version (for those that need that).

vite.config.ts (usually found at root)

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'

// Docs: https://vitejs.dev/config/
export default defineConfig({
  plugins: [svelte()],
  server: {
    proxy: {
      '/api': 'http://localhost:8080'
    }
  }
})

Upvotes: 5

brunnerh
brunnerh

Reputation: 184802

In the Vite config (vite.config.js) you can configure this via server.proxy, so it should be something like:

const config = {
    // ...
    server: {
        proxy: {
            '/api': 'http://localhost:8080',
        },
    },
};

Upvotes: 18

Related Questions