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

v1.0.4

Published

koa hostname router. glob match. middleware.

Downloads

8

Readme

koa-hostname

koa hostname 路由,支持 middleware 配置,主要用于单应用对接多域名(多业务线)

中间件机制可以结合 koa-router 完成 hostname + path 的路由

useage

测试时需要修改本地 host 配置

127.0.0.1 a.com x.a.com y.a.com b.com c.com

basic

基础用法

import Koa from 'koa';

import HRouter from '../src';

const app = new Koa();
const host = new HRouter();

host.use('a.com', ctx => ctx.body = 'a.com');

host.use('b.com', ctx => ctx.body = 'b.com');

app.use(host.middleware());

app.use(async ctx => {
  ctx.body = '404';
});

app.listen(8080);

通过

curl http://a.com:8080
curl http://b.com:8080
curl http://c.com:8080

可以得到返回

a.com
b.com
404

glob 匹配

支持 glob 语法匹配

import Koa from 'koa';

import HRouter from '../src';

const app = new Koa();
const host = new HRouter();

host.use(['a.com', '*.a.com'], ctx => ctx.body = 'a.com');

app.use(host.middleware());

app.use(async ctx => {
  ctx.body = '404 not found';
});

app.listen(8080);

通过

curl http://a.com:8080
curl http://x.a.com:8080
curl http://y.a.com:8080

可以得到返回

a.com
a.com
a.com

结合 koa-router

结合 koa-router,以及 koa 的中间件机制,可以完成完整的 hostname + path 路由匹配

import * as Koa from 'koa';
import * as Router from 'koa-router';
import { createReadStream } from 'fs';

import HRouter from '../src';

const app = new Koa();
const host = new HRouter();
const renderPage = (ctx: Koa.Context, pageName: string) => {
  ctx.set('content-type', 'text/html');
  ctx.body = createReadStream(__dirname + '/' + pageName + '.html');
}

const routerA = new Router();
routerA
  .get('/', ctx => renderPage(ctx, 'a'))
  .get('/api', ctx => ctx.body = 'hello a');
host.use(['a.com', '*.a.com'], async (ctx, next) => {
  console.log('site a.');
  await next();
}, routerA.routes());

const routerB = new Router();
routerB
  .get('/', ctx => renderPage(ctx, 'b'))
  .get('/api', ctx => ctx.body = 'hello b.');
host.use('b.com', async (ctx, next) => {
  console.log('site b.');
  await next();
}, routerB.routes());

const routerC = new Router();
routerC
  .get('/', ctx => renderPage(ctx, 'c'))
  .get('/api', ctx => ctx.body = 'hello c.');
host.use('c.com', async (ctx, next) => {
  console.log('site c.');
  await next();
}, routerC.routes());

app.use(host.middleware());

app.use(async ctx => {
  ctx.body = '404 not found';
});

app.listen(8080);

访问:

  • a.com,x.a.com,y.a.com 渲染页面 a
  • b.com 渲染页面 b
  • c.com 渲染页面 c
  • a.com/api,x.a.com/api,y.a.com/api 路由到 a 的接口 /api
  • b.com/api 路由到 b 接口 /api
  • c.com/api 路由到 c 接口 /api