Middleware có thể rewrite requests đến API khác: NextResponse.rewrite(new URL('/api/proxy', request.url)).
- Hoặc trong next.config.js dùng
rewritesarray để proxy:{ source: '/api/:path', destination: 'https://backend.com/:path' }. - Hữu ích để: hide API endpoints, implement BFF (Backend For Frontend), avoid CORS issues.
Middleware can rewrite requests to a different API: NextResponse.rewrite(new URL('/api/proxy', request.url)).
- Alternatively, use the
rewritesarray in next.config.js:{ source: '/api/:path', destination: 'https://backend.com/:path' }. - Useful for: hiding internal API endpoints, implementing a Backend For Frontend (BFF), and avoiding CORS issues.