How to configure redirects in vite?

Member

by lily , in category: Javascript , 8 months ago

How to configure redirects in vite?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , 8 months ago

@lily 

To configure redirects in Vite, you can create a vite.config.js file in the root of your project and add the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import { defineConfig } from 'vite';

export default defineConfig({
  server: {
    proxy: {
      '/api': 'http://localhost:3000',
    },
    port: 3000,
    open: true,
    fs: {
      strict: false,
    },
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
});


In this configuration, you can define the redirects in the server option by adding an object with the path you want to redirect and the target URL. For example, in the above code snippet, any requests to /api will be redirected to http://localhost:3000.


You can also configure other server options in the server object, such as the port number, whether to open the browser automatically, and file system options.


After adding the configuration, restart your Vite server for the changes to take effect.