• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Equim-chan/koa-virtual-host: A name-based virtual host middleware for Koa2.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Equim-chan/koa-virtual-host

开源软件地址:

https://github.com/Equim-chan/koa-virtual-host

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-virtual-host

npm version node version Build Status Coverage Status VersionEye Codacy Badge license

A name-based virtual host middleware for Koa2.

Installation

$ npm i --save koa-virtual-host

Example

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);

API

vhost(pattern, target)

  • 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

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 = 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

Test

$ npm test

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap