A Unified API Entry Point and Request Forwarding

Understand a unified API entry point, reuse Nginx reverse proxying to route requests, and distinguish its role from a BFF.

🎯 Introduction

We split users, products, and orders into independently running services and used a BFF to compose order details. The BFF on port 4000 now serves order details, while the order service on port 4003 creates orders.

Why should the frontend know these internal ports? If the order service changes address, must every frontend update its configuration?

No. The frontend can use one stable address, with a unified API entry point sending requests to the right service. This article explains that layer's architectural role and request forwarding, then verifies it using reverse proxying from the Nginx course. It does not reteach Nginx directives or add a Node.js forwarding program.


⚠️ Why Use a Unified API Entry Point?

In the current project, a frontend accessing internal services directly might call:

Query order details: GET  http://127.0.0.1:4000/bff/orders/o1
Create order:        POST http://127.0.0.1:4003/orders

The ports differ because the BFF and order service are separate processes, not because querying and creating orders inherently require different ports. The order service also exposes GET /orders/:id; the page calls the BFF because it needs combined data such as user and product names.

Connecting directly to multiple internal services introduces several issues:

  • The frontend must maintain multiple service addresses.
  • Moving internal services to different ports or servers may require frontend configuration changes.
  • Different clients may duplicate address management.

With a unified entry point, the frontend only accesses 8080:

Query order details: GET  http://127.0.0.1:8080/bff/orders/o1
Create order:        POST http://127.0.0.1:8080/api/orders

The frontend depends on stable public addresses and API paths instead of each service's internal address. Even if the order service moves to another machine, frontend calls stay the same as long as the public path remains unchanged.


🧱 What Is a Unified API Entry Point, and Where Does It Sit?

A unified API entry point is not a specific Node.js framework or plugin. It is the single access point where a system receives external API requests.

Nginx can fill that role here: it receives frontend requests, selects a service by path, and relays that service's response to the frontend.

courseComponents.mermaid.loading

The four branches represent possible forwarding targets. Each request is forwarded to one matching service based on its path, rather than calling all four. Earlier articles covered the BFF's internal queries and aggregation, so those are omitted from this diagram.


🔀 How Is a Request Forwarded to Its Target?

For order creation, the frontend sends:

POST /api/orders
Content-Type: application/json

{"userId":"u1","productId":"p1","quantity":1}

The flow has four steps:

  1. Receive the request: The entry point receives POST /api/orders on port 8080.
  2. Match a route: Configured path rules select the order service on 4003 and map public path /api/orders to internal path /orders.
  3. Forward and process: The entry point forwards the request. The order service queries the user and product, calculates the total, and creates the order as before.
  4. Return the response: The order service returns 201 and order JSON, which the entry point relays to the frontend.

courseComponents.mermaid.loading

The entry point only chooses where to send the request; it does not create orders or calculate totals. The order service still executes business logic.

This table summarizes the example's routes:

Frontend pathInternal target serviceExample internal path
/api/users/u1User service 4001/users/u1
/api/products/p1Product service 4002/products/p1
/api/ordersOrder service 4003/orders
/api/orders/o1Order service 4003/orders/o1
/bff/orders/o1BFF 4000/bff/orders/o1

Public and internal paths can differ as long as their mapping is configured. These URLs are conventions for this course, not requirements for every project.


⚖️ How Does the Entry Point Differ from a BFF?

Both sit between the frontend and business services, but solve different problems.

ComparisonUnified API entry pointBFF
Core questionWhich service should receive the request?What data does the page need?
Main workForward requests by path and relay responsesCall business APIs, aggregate data, and adapt it
Knowledge of order fieldsUsually unnecessaryMust know which fields the page uses
Course exampleForward /api/orders to the order serviceCompose order, user, and product data into page JSON

For example, /bff/orders/o1 goes through the entry point before reaching the BFF. The entry point does not compose data for the BFF, and the BFF need not forward all external requests.

They can run as separate processes, or be organized differently according to project size. Their responsibilities define the distinction, rather than technology names or port counts.


🛠 Reuse Nginx Reverse Proxying for the Entry Point

The earlier Nginx course introduced reverse proxying. Here we reuse that configuration knowledge to route services, rather than reteaching location and proxy_pass.

Use these rules in a local Nginx server configuration, assuming all four services run on the same machine:

server {
    listen 127.0.0.1:8080;

    location /api/users/ {
        proxy_pass http://127.0.0.1:4001/users/;
    }

    location /api/products/ {
        proxy_pass http://127.0.0.1:4002/products/;
    }

    # Create an order: match /api/orders exactly.
    location = /api/orders {
        proxy_pass http://127.0.0.1:4003/orders;
    }

    # Query an order: match paths under /api/orders/.
    location /api/orders/ {
        proxy_pass http://127.0.0.1:4003/orders/;
    }

    location /bff/ {
        proxy_pass http://127.0.0.1:4000/bff/;
    }
}

Separate matches for creation and individual queries correctly map both /api/orders and /api/orders/o1 while avoiding unrelated paths being treated as order endpoints.

Focus on how public addresses map to internal service addresses. For example:

/api/orders       → /orders on 4003
/api/orders/o1    → /orders/o1 on 4003
/bff/orders/o1    → /bff/orders/o1 on 4000

Ensure the user, product, order, and BFF Node.js services are running, then check and reload Nginx as taught in the Nginx course. There is no need to create gateway.mjs or start a fifth Node.js process.

This local example listens on 127.0.0.1:8080. A frontend on another machine cannot use this loopback-only address; adjust the listening address and access method for your deployment. Real projects also need appropriate HTTPS, authorization, and network access controls, which are outside this article's focus.


🧪 Verify the Unified Entry Point

Call the user service, BFF, and order service, with every request going through port 8080.

1. Query a user

curl -i http://127.0.0.1:8080/api/users/u1

Expect 200 with a body like:

{"id":"u1","name":"Lin","email":"lin@example.test"}

2. Query order details

curl -i 'http://127.0.0.1:8080/bff/orders/o1?view=mobile'

Expect 200 with the BFF's aggregated data:

{
  "id": "o1",
  "customerName": "Lin",
  "productName": "Mechanical Keyboard",
  "totalCents": 59800
}

3. Create an order

curl -i -X POST http://127.0.0.1:8080/api/orders \
  -H 'Content-Type: application/json' \
  -d '{"userId":"u1","productId":"p1","quantity":1}'

Using the earlier initial data, the first successful creation should return 201 with a total of 29900 fen. The order ID depends on the number of orders already in the running process.

4. Check path handling

# Missing order: forwarded to the order service, which returns 404.
curl -i http://127.0.0.1:8080/api/orders/missing

# Unconfigured public path: handled by Nginx, not automatically forwarded to a business service.
curl -i http://127.0.0.1:8080/api/unknown

With this Nginx configuration, unmatched /api/unknown typically returns Nginx's own 404 page, which may not use the business service's JSON error format. When debugging, first identify whether the entry point or the business service produced the error.

If a request fails, check the public path match, whether the target service is running, and the internal port mapping. These checks cover this article's practical goal.


❓ Questions and Answers

Q1: Must I build a Node.js gateway for a unified API entry point?

No. This is an architectural responsibility, not a prescribed technology. This course reuses Nginx reverse proxying. Consider a dedicated gateway solution only for specific needs beyond basic Nginx configuration, rather than writing a forwarding program by default.

Q2: Can all internal services be exposed publicly once there is a unified entry point?

They should not be exposed by default. The entry point gives frontends a stable public address; internal services typically accept traffic only from the entry point or other trusted services. External access depends on business and network requirements. The entry point also does not replace authorization checks within business services.


🧪 Exercise

Without changing the product service, expose it through a new public path:

Frontend: GET /api/catalog/p1
Target: GET http://127.0.0.1:4002/products/p1

Add a matching path mapping to the existing Nginx configuration, then verify:

  1. http://127.0.0.1:8080/api/catalog/p1 returns product information.
  2. http://127.0.0.1:8080/api/catalog/missing returns the product service's 404.
  3. Explain why adding a public path requires only an entry point configuration change, without modifying the product service.

Success criteria: Explain the relationship between the public path, internal target address, and business service.


🎉 You understand the role of a unified API entry point and can forward requests using existing reverse proxy capabilities!