npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

koa-virtual-host

v0.2.0

Published

A name-based virtual host engine for Koa2.

Readme

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