|
| 1 | +# Frontend Reverse Proxy Rule |
| 2 | + |
| 3 | +When implementing a frontend-backend separated web application, always configure a reverse proxy in the frontend to forward API requests to the backend server. |
| 4 | + |
| 5 | +## Background |
| 6 | + |
| 7 | +The current online preview environment can only expose a single port. Therefore, when developing applications with separate frontend and backend services, the frontend must include a reverse proxy configuration to route API requests to the backend. |
| 8 | + |
| 9 | +## Guidelines |
| 10 | + |
| 11 | +When implementing a frontend for a web application with a separate backend: |
| 12 | + |
| 13 | +1. **Configure reverse proxy**: Set up a proxy in the frontend development server to forward API requests to the backend |
| 14 | +2. **Use consistent API prefix**: Typically use `/api` as the prefix for backend requests |
| 15 | +3. **Ensure both services start**: The preview should start both frontend and backend services |
| 16 | + |
| 17 | +## Implementation by Framework |
| 18 | + |
| 19 | +### Vite (Vue, React, etc.) |
| 20 | + |
| 21 | +In `vite.config.ts` or `vite.config.js`: |
| 22 | + |
| 23 | +```typescript |
| 24 | +export default defineConfig({ |
| 25 | + server: { |
| 26 | + proxy: { |
| 27 | + '/api': { |
| 28 | + target: 'http://localhost:3001', // Backend server address |
| 29 | + changeOrigin: true, |
| 30 | + // rewrite: (path) => path.replace(/^\/api/, '') // Optional: remove /api prefix |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +### Create React App |
| 38 | + |
| 39 | +In `package.json`: |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "proxy": "http://localhost:3001" |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Or use `src/setupProxy.js` for more control: |
| 48 | + |
| 49 | +```javascript |
| 50 | +const { createProxyMiddleware } = require('http-proxy-middleware'); |
| 51 | + |
| 52 | +module.exports = function(app) { |
| 53 | + app.use('/api', createProxyMiddleware({ |
| 54 | + target: 'http://localhost:3001', |
| 55 | + changeOrigin: true, |
| 56 | + })); |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +### Next.js |
| 61 | + |
| 62 | +In `next.config.js`: |
| 63 | + |
| 64 | +```javascript |
| 65 | +module.exports = { |
| 66 | + async rewrites() { |
| 67 | + return [ |
| 68 | + { |
| 69 | + source: '/api/:path*', |
| 70 | + destination: 'http://localhost:3001/api/:path*', |
| 71 | + }, |
| 72 | + ]; |
| 73 | + }, |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +### Webpack Dev Server |
| 78 | + |
| 79 | +In `webpack.config.js`: |
| 80 | + |
| 81 | +```javascript |
| 82 | +module.exports = { |
| 83 | + devServer: { |
| 84 | + proxy: { |
| 85 | + '/api': { |
| 86 | + target: 'http://localhost:3001', |
| 87 | + changeOrigin: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +## Startup Script |
| 95 | + |
| 96 | +Create a startup script that runs both services. Example `start.sh`: |
| 97 | + |
| 98 | +```bash |
| 99 | +#!/bin/bash |
| 100 | +# Start backend server in background |
| 101 | +cd backend && npm run dev & |
| 102 | +BACKEND_PID=$! |
| 103 | + |
| 104 | +# Start frontend server (this will be the exposed port) |
| 105 | +cd frontend && npm run dev |
| 106 | + |
| 107 | +# Cleanup on exit |
| 108 | +trap "kill $BACKEND_PID" EXIT |
| 109 | +``` |
| 110 | + |
| 111 | +## Rationale |
| 112 | + |
| 113 | +- Online preview environment only exposes one port to the user |
| 114 | +- The frontend development server port is typically the one exposed for preview |
| 115 | +- Reverse proxy allows API requests to reach the backend without CORS issues |
| 116 | +- This setup mimics production deployment where a single entry point handles all requests |
0 commit comments