API gateway — single entry point for multiple services.
1const express = require("express");2const httpProxy = require("http-proxy");34const proxy = httpProxy.createProxyServer({});56const routes = {7 "/api/users": "http://user-service:3001",8 "/api/orders": "http://order-service:3002",9 "/api/products": "http://product-service:3003",10};1112const app = express();1314// Authentication middleware15app.use(async (req, res, next) => {16 const token = req.headers.authorization?.split(" ")[1];17 if (token) {18 try {19 req.user = jwt.verify(token, SECRET);20 } catch (err) {21 return res.status(401).json({ error: "Invalid token" });22 }23 }24 next();25});2627// Rate limiting28app.use(rateLimiter);2930// Route to services31Object.entries(routes).forEach(([path, target]) => {32 app.use(path, (req, res) => {33 proxy.web(req, res, { target }, (err) => {34 res.status(502).json({ error: "Service unavailable" });35 });36 });37});3839// Health check40app.get("/health", (req, res) => res.json({ status: "ok" }));4142app.listen(80);