koa-subdomain
Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains.
Installation
Install using npm:
npm install koa-subdomain --save
Usage
use with koa-router:
const Koa = ;const Subdomain = ;const Router = ; const app = ;const subdomain = ;const router = ; router; // one.example.comsubdomain; app;app;
more example:
const app = ;const subdomain = ; // one.example.comsubdomain; // two.example.comsubdomain; subdomain // a.one.example.com ; // b.one.example.com // example.comsubdomain; // *.example.comsubdomain; // *.one.example.comsubdomain; // one.*.example.comsubdomain; app;app;
Wildcard subdomains will be accessible under wildcardSubdomains
in the state of koa context.
const Koa = ;const Subdomain = ;const Router = ; const app = ;const subdomain = ;const router1 = ;const router2 = ; // get test.example.comrouter1; // get foo.bar.example.comrouter2; // *.example.comsubdomain;subdomain; app;app;
Note: Koa has a subdomainOffset
setting (2, by default), so the domain of the app is assumed to be the last two parts of the host. Here is an example when it is useful: if your app domain is localhost:3000
, you need to change subdomainOffset
to 1 for proper subdomain detection.
const app = ; appsubdomainOffset = 1 // one.localhost:3000subdomain;
koa1
Install:
npm install koa-subdomain@1 --save
Usage:
const app = ;const subdomain = ;const router = ; router; // one.example.comsubdomain; app;app;
Run test
git clone https://github.com/keenwon/koa-subdomain.gitcd koa-subdomainnpm installnpm test