From harness-claude
Implements Express.js API gateways or BFFs to route, aggregate, secure requests with auth, rate limiting, and proxying to microservices.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Route, aggregate, and secure client requests through an API gateway or BFF pattern.
Builds API gateways with routing, load balancing, rate limiting, authentication, circuit breakers, and health checks for multiple backend microservices.
Implements API gateway patterns for routing, authentication, rate limiting, and service composition in microservices. Covers BFF, service mesh, and tools like Nginx, Envoy, AWS.
Configures API gateways like Kong, Nginx, AWS API Gateway, Traefik, Node.js for routing, authentication, rate limiting, and request transformations in microservices and reverse proxies.
Share bugs, ideas, or general feedback.
Route, aggregate, and secure client requests through an API gateway or BFF pattern.
Custom API gateway with Express (for BFF pattern):
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
const app = express();
// --- Cross-cutting concerns — apply to all routes ---
// 1. Auth middleware
app.use(async (req, res, next) => {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
try {
req.user = await verifyJWT(token);
next();
} catch {
res.status(401).json({ error: 'Invalid token' });
}
});
// 2. Rate limiting
import rateLimit from 'express-rate-limit';
app.use(
rateLimit({
windowMs: 60_000,
max: 100,
standardHeaders: true,
keyGenerator: (req) => req.user?.id ?? req.ip,
})
);
// 3. Request ID
app.use((req, res, next) => {
req.id = (req.headers['x-request-id'] as string) ?? crypto.randomUUID();
res.setHeader('X-Request-Id', req.id);
next();
});
// --- Routing to backend services ---
app.use(
'/api/orders',
createProxyMiddleware({
target: process.env.ORDER_SERVICE_URL,
changeOrigin: true,
pathRewrite: { '^/api/orders': '' },
on: {
proxyReq: (proxyReq, req) => {
proxyReq.setHeader('X-User-Id', req.user!.id);
proxyReq.setHeader('X-Request-Id', req.id!);
},
error: (err, req, res) => {
(res as express.Response).status(503).json({ error: 'Service unavailable' });
},
},
})
);
app.use(
'/api/products',
createProxyMiddleware({
target: process.env.CATALOG_SERVICE_URL,
changeOrigin: true,
pathRewrite: { '^/api/products': '' },
})
);
BFF (Backend for Frontend) — aggregation:
// Mobile app needs order + product details in one call
// Without BFF: mobile makes 3 requests (order, products × N, user)
// With BFF: one call, gateway aggregates
app.get('/bff/mobile/order-detail/:orderId', async (req, res) => {
const { orderId } = req.params;
const userId = req.user!.id;
try {
// Parallel fetch from backend services
const [order, user] = await Promise.all([
orderServiceClient.getOrder(orderId),
userServiceClient.getUser(userId),
]);
// Verify ownership
if (order.userId !== userId) {
res.status(403).json({ error: 'Forbidden' });
return;
}
// Fetch product details for each item in parallel
const products = await Promise.all(
order.items.map((item) => catalogServiceClient.getProduct(item.productId))
);
// Shape the response for mobile — only what the app needs
res.json({
orderId: order.id,
status: order.status,
createdAt: order.createdAt,
customerName: user.name,
items: order.items.map((item, i) => ({
name: products[i].name,
imageUrl: products[i].imageUrl,
quantity: item.quantity,
price: item.unitPrice,
})),
total: order.total,
tracking: order.trackingNumber,
});
} catch (err) {
res.status(500).json({ error: 'Failed to load order' });
}
});
Service client with circuit breaker:
import CircuitBreaker from 'opossum';
class OrderServiceClient {
private breaker: CircuitBreaker;
constructor(private readonly baseUrl: string) {
this.breaker = new CircuitBreaker(this.rawFetch.bind(this), {
timeout: 5_000,
errorThresholdPercentage: 50,
resetTimeout: 30_000,
});
this.breaker.fallback(() => null);
}
async getOrder(orderId: string): Promise<Order | null> {
return this.breaker.fire(orderId) as Promise<Order | null>;
}
private async rawFetch(orderId: string): Promise<Order> {
const res = await fetch(`${this.baseUrl}/orders/${orderId}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}
}
Gateway vs. BFF: An API Gateway handles routing and cross-cutting concerns for all clients. A BFF (Backend for Frontend) is a specialized gateway for one client type (web BFF, mobile BFF), aggregating and shaping responses for that client's specific needs.
What belongs in the gateway:
What does NOT belong in the gateway:
Anti-patterns:
Managed options: AWS API Gateway, Kong, Nginx, Traefik, Envoy. Use these for production instead of building your own unless you have very specific BFF aggregation needs.
microservices.io/patterns/apigateway.html