A name-based virtual host middleware for Koa2.
$ npm i --save koa-virtual-host
const Koa = require('koa');
const vhost = require('koa-virtual-host');
// Import sub Koa apps
const api = require('./apps/api');
const apex = require('./apps/apex');
const blog = require('./apps/blog');
const forum = require('./apps/forum');
const resume = require('./apps/resume');
const unicode = require('./apps/unicode');
// Set up a host
const host = new Koa();
/**
* Configure vhosts
*/
// Support string patterns
host.use(vhost('blog.example.com', blog));
// Support regexp patterns
host.use(vhost(/^(?:pro.*|resume)\.example\.com$/i, resume));
// Support pattern-app mappings as object
host.use(vhost({
'example.org': apex,
'forum.example.com': forum
}));
// Support pattern-app mappings as array of objects
host.use(vhost([{
pattern: /^b(?:bs|oard)\.example\..+$/i,
target: forum
}, {
pattern: 'api.example.com',
target: api
}]));
// Support Unicode hostname
// Support many-to-one mappings
host.use(vhost('中文域名.com', unicode));
host.use(vhost(/(?:萌|moe)/, unicode));
host.use(vhost(/[\u4E00-\u9FFF]\.io/, unicode));
// Support basic global controls serving as a usual Koa app
host.use(async (ctx, next) => {
ctx.set('Server', 'Koa Virtual Host');
await next();
});
// Listen and enjoy
host.listen(80);
- pattern (
String
|RegExp
) - the pattern used to match the hostname - target (
Object
) - the Koa app
Example:
const Koa = require('koa');
const vhost = require('koa-virtual-host');
// You can also import them from existing modules that export ones.
const a = new Koa();
const b = new Koa();
a.use(async (ctx, next) => {
ctx.body = 'Hello';
await next();
});
b.use(async (ctx, next) => {
ctx.body = 'World';
await next();
});
const host = new Koa();
// The requests that match the pattern will be forwarded to the corresponding app.
host.use(vhost(/localhost/i, a));
host.use(vhost('127.0.0.1', b));
host.listen(8000);
Try to request:
$ curl http://localhost:8000/
Hello
$ curl http://127.0.0.1:8000/
World
- 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 = require('koa');
const vhost = require('koa-virtual-host');
const a = new Koa();
const b = new Koa();
const c = new Koa();
const d = new Koa();
a.use(async (ctx, next) => {
await next();
ctx.body += ' World';
});
b.use(async (ctx, next) => {
ctx.body = 'Hello';
await next();
});
c.use(async (ctx, next) => {
await next();
ctx.set('X-Powered-By', 'Koa');
});
d.use(async (ctx, next) => {
ctx.body = 'foobar';
await next();
ctx.set('X-Powered-By', 'vhost');
});
const host = new Koa();
host.use(vhost({
'localhost': a,
'127.0.0.1': c
}));
// Additionally, if the pattern is duplicated,
// the corresponding apps will be called in order
host.use(vhost([{
pattern: 'localhost',
target: b
}, {
pattern: /^127\.0\.0\.\d+$/,
target: d
}]));
host.listen(8000);
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
$ npm test