0
0
Expressframework~20 mins

API gateway concept in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
💼 Career
Progress0 / 4 steps
Set up the Express gateway with proxy middleware
Create an Express app that proxies /api/users requests to a user service running on port 3001.
Express
Need a hint?
Use app.use with createProxyMiddleware. The target is the downstream service URL. pathRewrite strips the /api/users prefix.
Add authentication middleware before proxying
Add a JWT verification middleware that runs before any proxy routes. If the token is invalid, return 401. If valid, attach the decoded user to the request headers.
Express
Need a hint?
Place app.use(authMiddleware) before the proxy routes. Extract the token from the Authorization header, verify it with jwt.verify, and attach decoded info as custom headers.
Add a second proxy route and rate limiting
Add a proxy route for /api/orders pointing to port 3002. Add rate limiting at the gateway level to allow max 100 requests per 15 minutes per IP.
Express
Need a hint?
Create a limiter with express-rate-limit and app.use it before the proxy routes. Add a second createProxyMiddleware for /api/orders targeting port 3002.
Create a response aggregation endpoint
Create a /api/dashboard endpoint that fetches data from both the user service and orders service in parallel and returns a combined response.
Express
Need a hint?
Use Promise.all to fetch from both services in parallel. Forward the x-user-id header so downstream services know which user is requesting. Wrap in try/catch and return 502 on failure.