Bird
Raised Fist0
Expressframework~20 mins

API gateway concept in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Gateway Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary role of an API Gateway in a microservices architecture?

Choose the best description of the main function of an API Gateway.

AIt acts as a single entry point that routes client requests to multiple backend services.
BIt stores all the data for the microservices in a centralized database.
CIt replaces the need for backend services by handling all business logic itself.
DIt directly connects clients to each microservice without any routing.
Attempts:
2 left
💡 Hint

Think about how clients interact with many services through one interface.

❓ Architecture
intermediate
2:00remaining
Which component is NOT typically handled by an API Gateway?

Select the feature that an API Gateway usually does NOT provide.

ARequest routing and load balancing
BAuthentication and authorization
CDirect database query execution
DResponse aggregation from multiple services
Attempts:
2 left
💡 Hint

Consider what tasks are usually done by backend services versus the gateway.

❓ scaling
advanced
2:00remaining
How does an API Gateway help improve scalability in a distributed system?

Choose the option that best explains how an API Gateway supports system scalability.

ABy storing all user sessions centrally in the gateway
BBy directly managing database connections for all services
CBy replacing backend services with a single monolithic service
DBy caching responses and reducing load on backend services
Attempts:
2 left
💡 Hint

Think about how reducing repeated work can help scale better.

❓ tradeoff
advanced
2:00remaining
What is a common tradeoff when using an API Gateway in system design?

Identify the main downside of introducing an API Gateway.

AIt can become a single point of failure if not designed for high availability.
BIt eliminates the need for backend services, increasing complexity.
CIt requires clients to connect directly to multiple services.
DIt reduces security by exposing all backend services publicly.
Attempts:
2 left
💡 Hint

Consider what happens if the gateway goes down.

❓ component
expert
2:00remaining
In an Express-based API Gateway, which middleware is best suited to handle authentication before routing requests?

Select the middleware type that should be used to authenticate requests in an Express API Gateway.

AA middleware that modifies response headers after sending the response
BA global middleware that checks tokens before any route handlers
CA route-specific middleware that runs after forwarding the request
DA middleware that only logs requests without blocking unauthorized ones
Attempts:
2 left
💡 Hint

Authentication should happen early to block unauthorized access.

Practice

(1/5)
1. What is the primary role of an API gateway in a system using Express?
easy
A. It directly serves static files to users.
B. It stores all the data for the application.
C. It acts as a single entry point to route requests to multiple backend services.
D. It replaces the database in the backend.

Solution

  1. Step 1: Understand the role of API gateway

    An API gateway acts as a single entry point that routes client requests to appropriate backend services.
  2. Step 2: Eliminate incorrect roles

    Storing data, replacing databases, or serving static files are not responsibilities of an API gateway.
  3. Final Answer:

    It acts as a single entry point to route requests to multiple backend services. -> Option C
  4. Quick Check:

    API gateway = single entry point [OK]
Hint: API gateway routes requests centrally [OK]
Common Mistakes:
  • Thinking API gateway stores data
  • Confusing API gateway with database
  • Assuming it serves static files directly
2. Which Express middleware is commonly used to forward requests in an API gateway setup?
easy
A. http-proxy-middleware
B. express.static()
C. body-parser
D. cors

Solution

  1. Step 1: Identify middleware for proxying requests

    The http-proxy-middleware package is used to forward requests from the gateway to backend services.
  2. Step 2: Differentiate from other middleware

    express.static() serves static files, body-parser parses request bodies, and cors manages cross-origin requests, none forward requests.
  3. Final Answer:

    http-proxy-middleware -> Option A
  4. Quick Check:

    Proxy middleware = http-proxy-middleware [OK]
Hint: Proxy requests with http-proxy-middleware [OK]
Common Mistakes:
  • Using express.static() to proxy requests
  • Confusing body-parser with proxying
  • Assuming cors forwards requests
3. Given this Express API gateway code snippet, what will be the output when a client requests /api/users?
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();

app.use('/api/users', createProxyMiddleware({ target: 'http://localhost:5000', changeOrigin: true }));

app.listen(3000);
medium
A. The request results in a 404 error
B. The request is served by Express app on port 3000
C. The request is blocked by CORS policy
D. The request is forwarded to http://localhost:5000/api/users

Solution

  1. Step 1: Analyze the proxy middleware setup

    The middleware forwards requests matching /api/users to http://localhost:5000, preserving the path.
  2. Step 2: Understand request forwarding behavior

    The request /api/users on port 3000 is proxied to http://localhost:5000/api/users.
  3. Final Answer:

    The request is forwarded to http://localhost:5000/api/users -> Option D
  4. Quick Check:

    Proxy forwards /api/users to target URL [OK]
Hint: Proxy forwards matching paths to target server [OK]
Common Mistakes:
  • Assuming Express serves the request itself
  • Expecting 404 without backend running
  • Confusing CORS blocking with proxy behavior
4. Identify the error in this Express API gateway code snippet:
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();

app.use('/api', createProxyMiddleware({ target: 'http://localhost:4000' }));

app.listen(3000);

// Client requests /api/users but gets no response
medium
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

  1. 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.
  2. Step 2: Verify other parts

    The target URL and path are correct, and app listens on port 3000, so those are not errors.
  3. Final Answer:

    Missing changeOrigin: true causes backend to reject requests -> Option A
  4. 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

  1. Step 1: Identify routing and logging needs

    The API gateway must route requests to multiple backends and log method and URL of each request.
  2. Step 2: Choose appropriate middleware

    http-proxy-middleware handles routing, and a custom Express middleware can log requests before proxying.
  3. 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.
  4. Final Answer:

    Use http-proxy-middleware for routing and add a custom middleware before it to log requests. -> Option B
  5. Quick Check:

    Proxy + custom middleware = routing + logging [OK]
Hint: Log requests before proxying with custom middleware [OK]
Common Mistakes:
  • Using express.static() for routing
  • Skipping gateway for routing
  • Confusing body-parser with routing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard