Back to results for “Architecture & Design Patterns”
Node.js › Microservices Architecture with Node.js

What is an API Gateway, and what role does it play in front of a collection of Node.js microservices?

Intermediate

Answer

An API Gateway acts as a single entry point for external clients, routing incoming requests to the appropriate backend microservice, while also centralizing cross-cutting concerns like authentication, rate limiting, request/response transformation, and aggregating responses from multiple services into a single response tailored for a specific client (like a mobile app needing a combined payload from three separate services) -- sparing individual microservices and external clients from needing to handle all of this routing and cross-cutting logic themselves.
// Simplified API Gateway routing logic
app.use('/api/users', authenticate, proxy('http://user-service:3001'));
app.use('/api/orders', authenticate, proxy('http://order-service:3002'));
app.use('/api/products', proxy('http://product-service:3003')); // no auth needed for public data
Real-world example A mobile app's dashboard screen calls a single API Gateway endpoint that internally fans out to the user, order, and notification microservices in parallel and combines their responses into one tailored payload, sparing the mobile client from needing to make and coordinate three separate network requests itself.

Common follow-ups: How does an API Gateway avoid becoming a single point of failure or a performance bottleneck for the entire system?;What's the difference between a general-purpose API Gateway product (like Kong or AWS API Gateway) and a hand-rolled Express-based gateway?