koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
849 stars 174 forks source link

feat: allow set router host match #156

Closed yeliex closed 2 years ago

yeliex commented 2 years ago

If a server with many hosts and the same path in each host may different.

current version:

router.get('/', (ctx) => {
    if(ctx.host === 'a') // ...
    if(ctx.host === 'b') // ...
})

it is a good way to support host match in router definition:

// hosta.router.js
const routerA = new Router({
  host: 'hosta.com' // only match if request host exactly equal `hosta.com`
});
router.get('/', (ctx, next) => {
  // Response for hosta.com
});

// hostb.router.js
const routerB = new Router({
  host: /^(.*\.)?hostb\.com$/ // match all subdomains of hostb.com, including hostb.com, www.hostb.com, etc.
});
router.get('/', (ctx, next) => {
  // Response index for matched hosts
});

Checklist

yeliex commented 2 years ago

@titanism @etroynov pls

titanism commented 2 years ago

v11.0.1 released

https://github.com/koajs/router/releases/tag/v11.0.1