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 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-switch-vhost

v0.1.0

Published

A simple and performant virtual-host middleware for Koa

Downloads

3

Readme

koa-switch-vhost

Installation

$ yarn add koa-switch-vhost

Example

const Koa = require('koa');
const Switch = require('koa-switch-vhost');

// Import sub-apps
const api = require('./apps/api');
const moe = require('./apps/moe');
const blog = require('./apps/blog');
const forum = require('./apps/forum');
const unicode = require('./apps/unicode');

// Set up a host
const host = new Koa();

// Set up vhost switch
const vhost = new Switch();

/**
 * Configure vhosts
 * Currently only supports pattern-app mappings only as an array of objects.
 * Unlike in most of other vhost middleware, hostnames should be strings.
 */

// Supports pattern-app mappings as an object
vhost.use({
  host: 'blog.example.com',
  app: blog
});

// Supports pattern-app mappings as array of objects and many-to-one mappings
vhost.use([
  {
    host: 'bbs.example.com',
    app: forum
  },
  {
    host: 'forum.example.com',
    app: forum
  }
]);

// Supports pattern-app mappings as a argument list of objects
vhost.use({
  host: 'board.example.com',
  app: forum
}, {
  host: 'api.example.com',
  app: api
});


// Only supports RFC1123 hostnames. @NOTE: Any of these will throw a TypeError
vhost.use({
  host: '中文域名.com',
  app: unicode
});

// @NOTE: This will throw because one of the supplied hosts is not RFC-compliant
vhost.use({
  host: '萌.io',
  app: moe
}, {
  host: 'moe.io',
  app: moe
});

// Supports basic global controls serving as regular Koa middleware
host.use(async (context, next) => {
  context.set('Server', 'Koa');
  await next();
});

// Switch will skip to next middleware if there was no app for the supplied host
host.use(vhost.switch());
host.use((context) => {
  context.status = 404;
  context.body = 'nothing matched';
});

// Listen and enjoy
host.listen(80);

API

new Switch(config)

  • config (Object) - configuration
    • config.trim_www? (Bool) - specifies whether the switch should trim www. before matching the host. Default: true.
    • config.vhosts? (Array) - array of vhost data objects. Default: [].

Example:

const Switch = require('koa-switch-vhost');

// You can disable `www.` trimming for hostnames
const vhost = new Switch({ trim_www: false });

// You can also supple vhosts to be added on Switch initialization
const vhost2 = new Switch({
  vhosts: [
    {
      host: 'bbs.example.com',
      app: forum
    },
    {
      host: 'forum.example.com',
      app: forum
    }
  ]
});

// Or you can do both at the same time if you wish so
const supervhost = new Switch({
  trim_www: false,
  vhosts: [
    {
      host: 'bbs.example.com',
      app: forum
    },
    {
      host: 'forum.example.com',
      app: forum
    }
  ]
});

Switch#use({ vhost data object })

Vhost data object must have exactly two parameters:

  • host (String) - host used to match the hostname
  • app (Object | Array) - Koa app, Koa middleware or an array of Koa middleware

Example:

const Koa = require('koa');
const Switch = require('koa-switch-vhost');

// Importing sub-apps
const api = require('./apps/api');
const moe = require('./apps/moe');
const blog = require('./apps/blog');

const vhost = new Switch();

vhost.use({
  host: 'blog.example.com',
  app: blog
});

vhost.use({
  host: 'api.example.com',
  app: api
});

vhost.use({
  host: 'example.moe',
  app: moe
});

const host = new Koa();

// The requests that match the hosts will be forwarded to the corresponding apps.
host.use(vhost.switch());
host.listen(8000);

Switch#use(...{ vhost data object })

  • data objects (...Object | Array(Object)) - a list of data objects, either an array or as arguments. See previous section for data object reference.

Example:

const Koa = require('koa');
const Switch = require('koa-switch-vhost');

const vhost = new Switch();

vhost.use({
  host: 'localhost',
  async app(context, next) {
    ctx.body += 'Hello, World';
    await next();
  }
}, {
  host: '127.0.0.1',
  async app(context, next) {
    await next();
    ctx.set('X-Powered-By', 'vhost');
  }
});

// Passing an array instead of listing data objects as arguments is valid too.
// @NOTE: This one will throw an error because apps for both `localhost` and `127.0.0.1`
// had already been assigned.
vhost.use([
  {
    host: 'localhost',
    async app(context, next) {
      await next();
      ctx.set('X-Powered-By', 'Koa');
    }
  },
  {
    host: '127.0.0.1',
    async app(context, next) {
      ctx.body = 'foobar';
      await next();
    }
  }
]);

const host = new Koa();

// The requests that match the hosts will be forwarded to the corresponding apps.
host.use(vhost.switch());
host.listen(8000);

Test

$ yarn test

License

3-Clause BSD