Endpoints represent backend servers that receive load-balanced traffic. There are two types:
- Endpoint - Standard endpoint for basic load balancing
- GeoEndpoint - Endpoint with geographic targeting for geo steering
import { Endpoint } from "worker-lb";
const endpoint = new Endpoint("https://api.example.com");The trailing slash is automatically removed from URLs.
const endpoint = new Endpoint("https://api.example.com", {
healthCheckPathname: "/health", // the default is "/"
});The health check performs a GET request to the specified path and considers the endpoint healthy if it returns a 2xx status code.
| Option | Type | Default | Description |
|---|---|---|---|
healthCheckPathname |
`/${string}` |
"/" |
Path for health check requests (e.g., "/health") |
Forwards a request to this endpoint. Used internally by the LoadBalancer.
const endpoint = new Endpoint("https://api.example.com");
const response = await endpoint.commitRequest(request);Performs a health check against this endpoint. Returns true if healthy.
const endpoint = new Endpoint("https://api.example.com", {
healthCheckPathname: "/health",
});
const isHealthy = await endpoint.healthCheck();GeoEndpoint extends Endpoint with geographic targeting configuration. Used with geo steering to route requests based on client location.
import { GeoEndpoint } from "worker-lb";
const endpoint = new GeoEndpoint("https://eu.api.example.com", {
geo: { type: "continent", continents: ["EU"] },
});GeoEndpoint accepts all Endpoint options plus a required geo configuration:
| Option | Type | Default | Description |
|---|---|---|---|
healthCheckPathname |
`/${string}` |
"/" |
Path for health check requests (e.g., "/health") |
geo |
GeoConfig |
— | Geographic targeting configuration (required) |
new GeoEndpoint("https://eu.api.example.com", {
geo: { type: "continent", continents: ["EU", "AF"] },
});Continent codes: AF, AN, AS, EU, NA, OC, SA
new GeoEndpoint("https://de.api.example.com", {
geo: { type: "country", countries: ["DE", "AT", "CH"] },
});Uses ISO 3166-1 alpha-2 codes. Use "T1" for TOR requests.
new GeoEndpoint("https://west.api.example.com", {
geo: { type: "region", regions: ["CA", "WA", "OR"] },
});Uses ISO 3166-2 region/subdivision codes.
new GeoEndpoint("https://dfw.api.example.com", {
geo: { type: "colo", colos: ["DFW", "IAH", "AUS"] },
});Uses Cloudflare data center codes (IATA airport codes).
GeoEndpoint inherits all Endpoint methods plus:
Checks if this endpoint matches the request's geographic location.
const endpoint = new GeoEndpoint("https://eu.api.example.com", {
geo: { type: "continent", continents: ["EU"] },
});
const matches = endpoint.matchesRequest(request);import { Endpoint, LoadBalancer } from "worker-lb";
const lb = new LoadBalancer({
endpoints: [
new Endpoint("https://api1.example.com"),
new Endpoint("https://api2.example.com"),
],
});import { GeoEndpoint, LoadBalancer } from "worker-lb";
const lb = new LoadBalancer({
endpoints: [
new GeoEndpoint("https://us.api.example.com", {
geo: { type: "continent", continents: ["NA"] },
}),
new GeoEndpoint("https://eu.api.example.com", {
geo: { type: "continent", continents: ["EU"] },
}),
],
steering: { type: "geo" },
});See Geo Steering for detailed geo steering documentation.