app.subdomainOffset

Controls how many dot-separated parts are removed from the right of the hostname when computing `ctx.subdomains`.

Since Koa 2 Spec ↗

Syntax

app.subdomainOffset = number

Parameters

NameTypeRequiredDescription
value number Yes Number of TLD segments to ignore. Defaults to `2` (e.g. for `api.example.com` the subdomain is `["api"]`).

Returns

void — Setter; returns nothing.

Examples

import Koa from 'koa';

const app = new Koa();
app.subdomainOffset = 2; // default

app.use(async (ctx) => {
  // Request: GET http://tenant.api.example.com/
  ctx.body = { subdomains: ctx.subdomains };
});

app.listen(3000);
Output
{"subdomains":["api","tenant"]}
app.subdomainOffset = 3; // ignore .co.uk TLD

// Request: GET http://tenant.example.co.uk/
// ctx.subdomains → ["tenant"]
Output
{"subdomains":["tenant"]}

Notes

`ctx.subdomains` is an array ordered from most-specific to least-specific subdomain. Adjust `subdomainOffset` for multi-level TLDs (e.g. `.co.uk` requires offset `3`). Relies on `ctx.host`, so enable `app.proxy` if behind a load balancer.

See also