koa-virtual-host
A name-based virtual host middleware for Koa2.
Installation
$ npm i --save koa-virtual-host
Example
const Koa = ;const vhost = ;// Import sub Koa appsconst api = ;const apex = ;const blog = ;const forum = ;const resume = ;const unicode = ;// Set up a hostconst host = ;/*** Configure vhosts*/// Support string patternshost;// Support regexp patternshost;// Support pattern-app mappings as objecthost;// Support pattern-app mappings as array of objectshost;// Support Unicode hostname// Support many-to-one mappingshost;host;host;// Support basic global controls serving as a usual Koa apphost;// Listen and enjoyhost;
API
vhost(pattern, target)
- pattern (
String
|RegExp
) - the pattern used to match the hostname - target (
Object
) - the Koa app
Example:
const Koa = ;const vhost = ;// You can also import them from existing modules that export ones.const a = ;const b = ;a;b;const host = ;// The requests that match the pattern will be forwarded to the corresponding app.host;host;host;
Try to request:
$ curl http://localhost:8000/Hello$ curl http://127.0.0.1:8000/World
vhost(patterns)
- patterns (
Object
|Array
) - the pattern-app map
Notes that if you pass an Object
to patterns
, RegExp patterns will not be supported. To enable RegExp support, you need to pass an Array
.
Example:
const Koa = ;const vhost = ;const a = ;const b = ;const c = ;const d = ;a;b;c;d;const host = ;host;// Additionally, if the pattern is duplicated,// the corresponding apps will be called in orderhost;host;
Try to request:
$ curl http://localhost:8000/Hello World$ curl http://127.0.0.1:8000/foobar$ curl -I http://127.0.0.1:8000/ 2>&1 | grep "X-Powered-By"X-Powered-By: Koa
Test
$ npm test