A. Missing changeOrigin: true causes backend to reject requests
B. The target URL is incorrect and unreachable
C. The middleware is not mounted on the correct path
D. Express app is not listening on port 3000
Solution
Step 1: Check proxy middleware options
Without changeOrigin: true, the Host header sent to backend remains the gateway's, which may cause backend to reject requests.
Step 2: Verify other parts
The target URL and path are correct, and app listens on port 3000, so those are not errors.
Final Answer:
Missing changeOrigin: true causes backend to reject requests -> Option A
Quick Check:
changeOrigin fixes Host header issues [OK]
Hint: Add changeOrigin: true to fix backend rejection [OK]
Common Mistakes:
Ignoring changeOrigin option
Assuming wrong target URL without checking
Thinking app.listen is missing
5. You want to design an Express API gateway that routes requests to multiple backend services and also logs each request's method and URL. Which approach best fits this requirement?
hard
A. Use only express.static() to serve all backend services and log requests.
B. Use http-proxy-middleware for routing and add a custom middleware before it to log requests.
C. Implement routing logic inside each backend service and skip the gateway.
D. Use body-parser middleware to route and log requests.
Solution
Step 1: Identify routing and logging needs
The API gateway must route requests to multiple backends and log method and URL of each request.
Step 2: Choose appropriate middleware
http-proxy-middleware handles routing, and a custom Express middleware can log requests before proxying.
Step 3: Eliminate incorrect options
express.static() serves static files only, backend routing skips gateway, and body-parser parses bodies but does not route or log.
Final Answer:
Use http-proxy-middleware for routing and add a custom middleware before it to log requests. -> Option B