Sometimes an API request needs a small piece of logic before it reaches the backend.
You may want to check a request header, normalize a URL, add a response header, redirect a request, or apply a simple rule based on information available at the edge.
A common approach is to send every request to the application first and handle that logic inside the API.
That works, but it is not always necessary.
Azure Front Door supports edge-side request processing through its rules and edge computing capabilities. With the JavaScript-based edge functionality, you can run JavaScript logic close to the point where the request enters the Azure network before the request reaches your backend.
This can be useful when the logic is small, deterministic, and does not require access to your application's database or internal services.
The important part is knowing what belongs at the edge and what should remain inside the API.
What Edge Processing Means
In a traditional API request, the flow looks like this:
Client
|
v
Azure Front Door
|
v
API
|
v
DatabaseIf the API needs to perform a simple request transformation, the request has already traveled to the backend before the logic executes.
With edge processing, some logic can run earlier:
Client
|
v
Azure Front Door
|
v
Edge JavaScript
|
v
API
|
v
DatabaseThe difference is small in terms of architecture but useful in terms of request processing.
A request that can be rejected, redirected, or modified at the edge does not necessarily need to reach the application.
Where Edge JavaScript Fits
Edge JavaScript is not intended to replace your API.
It is better suited for small pieces of request and response processing.
Examples include:
Checking request headers
Adding headers
Modifying headers
Redirecting requests
Rewriting request information
Blocking simple unwanted requests
Applying lightweight routing logic
Normalizing request values
Returning a response without calling the backend
A useful rule is:
Simple request logic
↓
Edge
Business logic
↓
API
Persistent state
↓
Database / StorageThis separation keeps the edge layer manageable.
A Simple Example
Imagine an API that expects a custom request header called X-Client-Version.
Without edge processing, the API might receive every request and perform the validation itself:
var version = request.Headers["X-Client-Version"];
if (string.IsNullOrWhiteSpace(version))
{
return Results.BadRequest("Client version is required.");
}With edge processing, the request can be checked before it reaches the API.
Conceptually, the edge function can inspect the header:
export async function main(request) {
const version = request.headers.get("X-Client-Version");
if (!version) {
return new Response("Client version is required.", {
status: 400
});
}
return request;
}The exact JavaScript API available depends on the Azure Front Door edge execution capability and its runtime contract. The important architectural idea is that the request can be evaluated before the backend handles it.
Why Run Logic at the Edge?
The main advantage is that some decisions can happen before the request reaches your application.
Consider a request that should immediately be rejected.
Without edge processing:
Client
↓
Front Door
↓
API
↓
Validate
↓
RejectWith edge processing:
Client
↓
Front Door
↓
Validate
↓
RejectThe backend never needs to process the request.
This can reduce unnecessary application traffic and keep simple edge concerns out of application code.
Do Not Move Business Logic to the Edge
It can be tempting to put more and more application logic into edge JavaScript.
That usually creates problems.
For example, this is not a good candidate for edge execution:
Check customer balance
↓
Query database
↓
Calculate discount
↓
Create order
↓
Update inventoryThat belongs in the application layer.
The edge is better suited for logic that can make a decision from the request or response itself.
A good edge function should generally be:
Small
Fast
Stateless
Easy to understand
Independent of application internals
Request Headers Are a Good Use Case
Headers are one of the simplest things to inspect at the edge.
Suppose your application receives traffic from several client applications.
You could inspect:
User-Agent
X-Client-Version
Accept-Language
Authorization
Custom application headersFor example:
export async function main(request) {
const clientVersion =
request.headers.get("X-Client-Version");
if (clientVersion === "legacy") {
return new Response(null, {
status: 301,
headers: {
Location: "/upgrade"
}
});
}
return request;
}This keeps a simple compatibility rule away from the main API.
However, authentication decisions should be designed carefully. Checking whether a header exists is not the same thing as securely validating an identity.
Adding Response Headers
Edge processing can also be useful when a response needs a standard header.
For example, an application may need a response header such as:
X-Application-RegionThe edge layer can add or modify response information without requiring every backend service to implement the same code.
Conceptually:
export async function main(request) {
const response = await fetch(request);
response.headers.set(
"X-Application-Region",
"edge"
);
return response;
}The exact response handling depends on the supported Front Door JavaScript execution model.
The architectural benefit is that cross-cutting HTTP behavior can be centralized at the edge.
Redirects Are Another Good Candidate
Redirects are often a good fit for edge processing because they do not require backend application logic.
For example, suppose an old URL needs to redirect to a new location.
The edge can make the decision:
export async function main(request) {
const url = new URL(request.url);
if (url.pathname === "/old-api") {
return new Response(null, {
status: 301,
headers: {
Location: "/new-api"
}
});
}
return request;
}The client receives the redirect without the API needing to execute.
For large redirect maps, however, consider whether a dedicated routing or redirect mechanism is more appropriate than maintaining a large JavaScript file.
URL Normalization
Different clients sometimes generate slightly different URLs.
For example:
/api/orders
/api/orders/
/API/ordersIf your backend expects one consistent format, edge logic can normalize the request before it reaches the API.
A simple normalization might look like:
export async function main(request) {
const url = new URL(request.url);
if (url.pathname.endsWith("/")) {
url.pathname = url.pathname.slice(0, -1);
}
return request;
}The exact implementation should match the request mutation APIs provided by the Front Door runtime.
The main principle is to perform simple normalization once rather than duplicating it across multiple APIs.
Use Edge Processing for Cross-Cutting Concerns
Suppose an organization operates several APIs:
/api/orders
/api/customers
/api/payments
/api/inventoryEach API may need the same basic request behavior.
Without an edge layer, that logic can appear in every service:
Orders API
↓
Check header
Customers API
↓
Check header
Payments API
↓
Check header
Inventory API
↓
Check headerAn edge layer can centralize simple behavior:
Azure Front Door
|
Edge JavaScript
|
+-------------+-------------+
| | |
Orders Customers PaymentsThis can reduce duplication.
However, centralization also means a mistake at the edge can affect multiple APIs, so the edge code should remain small and well tested.
Edge Code Should Be Stateless
Avoid storing request-specific information in global variables.
For example, do not design edge code around:
let currentUser = null;Then assign a value during a request.
Join the conversation! Your thoughts help the community grow.