Configuring proxy in Vite is a straightforward process that can be achieved by modifying the vite.config.js file in your Vite project. In Vite, proxy configuration is handled via the proxy option within the server configuration. Here's a basic example of configuring proxy:
First, open the vite.config.js file in the root directory of your Vite project.
Then, add the server section to the configuration object and configure the proxy option within it:
javascript// vite.config.js import { defineConfig } from 'vite'; export default defineConfig({ server: { proxy: { // Using string shorthand notation // All requests starting with '/api' will be proxied to http://example.com '/api': 'http://example.com', // You can also provide detailed configuration '/another-api': { target: 'http://another-server.com', changeOrigin: true, rewrite: (path) => path.replace(/^/another-api/, '') } } } });
In the above configuration:
/api: This is a simple proxy configuration where all requests starting with/apiare forwarded tohttp://example.com./another-api: This is a more detailed configuration that includes the target server address, setschangeOrigintotrue(which is typically necessary for hostname checks), and provides arewritefunction that replaces the/another-apiprefix in the request path with an empty string, so the original proxy path is not included when forwarding to the target server.
You can define multiple proxy rules in the configuration, matching and forwarding requests based on your needs.
Remember to restart the Vite development server after modifying vite.config.js to apply the changes. Vite's proxy functionality is based on http-proxy and allows you to easily proxy specific API requests to other servers.
Here's another example of configuring proxy in vite.config.js:
javascript// vite.config.js import { defineConfig } from 'vite'; export default defineConfig({ server: { proxy: { // Using string shorthand notation '/api': 'http://localhost:3000', // All requests to `/api` will be proxied to `http://localhost:3000/api` // Using object notation for more configuration options '/api2': { target: 'http://localhost:3001', // Target server changeOrigin: true, // Controls the value of the host field in request headers rewrite: (path) => path.replace(/^/api2/, ''), // Rewrites the request path, removing `/api2` // Additional http-proxy options can be added here }, // Add other proxy rules for different paths '/other-path': { target: 'http://other-server.com', changeOrigin: true, // Other configurations... } } } });
In the above example, we have configured three proxy rules:
- When the request path starts with
/api, the request is proxied tohttp://localhost:3000with the request path unchanged (e.g.,/api/userbecomeshttp://localhost:3000/api/user). - For requests starting with
/api2, the request is proxied tohttp://localhost:3001with the/api2prefix removed (e.g.,/api2/userbecomeshttp://localhost:3001/user). - The last rule proxies requests starting with
/other-pathtohttp://other-server.com.
With this configuration, you can set up appropriate proxy rules to handle backend API requests during local development. This resolves cross-origin issues and simulates a production environment with frontend-backend separation.