Gateway API – Practice Question
Design and implement a simple API Gateway setup using Node.js (Express) with the following requirements.
Backend Services
Create a Node.js Express application exposing two backend services:
POST /usersAccepts a JSON payload to create a user Example:
json{ "name": "John Doe", "email": "john@example.com" }Returns HTTP
201 Createdwith the created user object.
GET /users- Returns a list of users in JSON format.
GET /postsReturns a list of posts in JSON format. Example:
json[ { "id": 1, "title": "First Post" }, { "id": 2, "title": "Second Post" } ]
Backend services must run on an internal port and must not be directly accessible from outside the gateway.
Gateway Requirements
Implement an API Gateway with the following characteristics:
Supports both HTTP and HTTPS
Performs TLS termination at the gateway
Uses
gateway.ioas the hostIncludes a header filter mechanism to validate or inspect incoming headers
Supports routing based on:
- HTTP method
- Request headers
- Request path
After TLS termination, traffic to backend services must use plain HTTP.
Header Filtering Rules
Implement header validation logic in the gateway with the following rules:
Mandatory headers:
x-api-key- Required for all gateway routes
- Missing header must result in
401 Unauthorized
x-client-id- Required for all
/api/v1/*routes - Missing header must result in
400 Bad Request
- Required for all
For all
POSTrequests:Content-Typemust beapplication/json- Invalid content type must result in
415 Unsupported Media Type
Gateway Routes
Configure the following gateway routes:
User Creation Route
Incoming request:
POST /userConditions:
- HTTP method must be
POST x-api-keyheader must be presentContent-Typemust beapplication/json
- HTTP method must be
Behavior:
- Forward the request to the backend
POST /usersservice
- Forward the request to the backend
Get Users Route
Incoming request:
GET /api/v1/usersConditions:
x-api-keyandx-client-idheaders must be present
Behavior:
- Rewrite the URL by removing
/api/v1 - Forward the request to the backend
GET /usersservice
- Rewrite the URL by removing
Catalog Redirect
Incoming request:
/api/v1/catalogBehavior:
- Internally redirect to
/api/v1/posts - Apply the same
/api/v1URL rewrite rules - Forward the request to the backend
GET /postsservice
- Internally redirect to
External Redirect
Incoming request:
/googleBehavior:
- Issue an HTTP redirect (
301or302) - Redirect the client to
https://google.com
- Issue an HTTP redirect (
Additional Notes
- All external traffic must be handled exclusively through the gateway.
- Backend services must not expose ports publicly.
- Gateway logic (TLS termination, routing, filtering, rewriting, redirects) must be clearly separated from backend service logic.
- The solution must be structured to allow easy addition of new routes and filters.