proxy.ts91 lines, 2.5 KB
| 1 | import { Router } from "express"; |
| 2 | import { createProxyMiddleware } from "http-proxy-middleware"; |
| 3 | import { servicesConfig } from "../config/services"; |
| 4 | |
| 5 | const router = Router(); |
| 6 | |
| 7 | function createServiceProxy( |
| 8 | targetUrl: string, |
| 9 | pathRewrite?: Record<string, string> |
| 10 | ): any { |
| 11 | const options = { |
| 12 | target: targetUrl, |
| 13 | changeOrigin: true, |
| 14 | pathRewrite: pathRewrite || {}, |
| 15 | timeout: 30000, // 30 seconds |
| 16 | proxyTimeout: 30000, // 30 seconds |
| 17 | onError: (err: any, req: any, res: any) => { |
| 18 | console.error(`Proxy error: ${err.message}`); |
| 19 | if (!res.headersSent) { |
| 20 | res.status(503).json({ |
| 21 | success: false, |
| 22 | error: "Service unavailable. Please try again later.", |
| 23 | message: "Service unavailable. Please try again later.", |
| 24 | }); |
| 25 | } |
| 26 | }, |
| 27 | onProxyReq: (proxyReq: any, req: any) => { |
| 28 | // Log proxy request details |
| 29 | console.log( |
| 30 | `Proxying request: ${req.method} ${req.originalUrl} to ${targetUrl}` |
| 31 | ); |
| 32 | |
| 33 | // Forward user information if available |
| 34 | if (req.user) { |
| 35 | proxyReq.setHeader("x-user-id", req.user.userId); |
| 36 | proxyReq.setHeader("x-user-email", req.user.email); |
| 37 | } |
| 38 | |
| 39 | // Ensure content type for POST/PUT requests |
| 40 | if ( |
| 41 | req.body && |
| 42 | (req.method === "POST" || |
| 43 | req.method === "PUT" || |
| 44 | req.method === "PATCH") |
| 45 | ) { |
| 46 | const bodyData = JSON.stringify(req.body); |
| 47 | proxyReq.setHeader("Content-Type", "application/json"); |
| 48 | proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData)); |
| 49 | proxyReq.write(bodyData); |
| 50 | } |
| 51 | }, |
| 52 | onProxyRes: (proxyRes: any, req: any) => { |
| 53 | // log proxy response details |
| 54 | console.log( |
| 55 | `Received response from ${targetUrl}: ${proxyRes.statusCode} for ${req.method} ${req.originalUrl}` |
| 56 | ); |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | return createProxyMiddleware(options); |
| 61 | } |
| 62 | |
| 63 | router.use( |
| 64 | "/api/auth", |
| 65 | createServiceProxy(servicesConfig.auth.url, { |
| 66 | "^/api/auth": "/auth", // Rewrite path to match service endpoint |
| 67 | }) |
| 68 | ); |
| 69 | |
| 70 | router.use( |
| 71 | "/api/users", |
| 72 | createServiceProxy(servicesConfig.users.url, { |
| 73 | "^/api/users": "/users", // Rewrite path to match service endpoint |
| 74 | }) |
| 75 | ); |
| 76 | |
| 77 | router.use( |
| 78 | "/api/notes", |
| 79 | createServiceProxy(servicesConfig.notes.url, { |
| 80 | "^/api/notes": "/notes", // Rewrite path to match service endpoint |
| 81 | }) |
| 82 | ); |
| 83 | |
| 84 | router.use( |
| 85 | "/api/tags", |
| 86 | createServiceProxy(servicesConfig.tags.url, { |
| 87 | "^/api/tags": "/tags", // Rewrite path to match service endpoint |
| 88 | }) |
| 89 | ); |
| 90 | |
| 91 | export default router; |