4.0.0 • Published 6 years ago

koa-mount v4.0.0

Weekly downloads
209,039
License
MIT
Repository
github
Last release
6 years ago

koa-mount

Mount other Koa applications as middleware. The path passed to mount() is stripped from the URL temporarily until the stack unwinds. This is useful for creating entire apps or middleware that will function correctly regardless of which path segment(s) they should operate on.

Installation

$ npm install koa-mount

Examples

View the ./examples directory for working examples.

Mounting Applications

Entire applications mounted at specific paths. For example you could mount a blog application at "/blog", with a router that matches paths such as "GET /", "GET /posts", and will behave properly for "GET /blog/posts" etc when mounted.

const mount = require('koa-mount');
const Koa = require('koa');

// hello

const a = new Koa();

a.use(async function (ctx, next){
  await next();
  ctx.body = 'Hello';
});

// world

const b = new Koa();

b.use(async function (ctx, next){
  await next();
  ctx.body = 'World';
});

// app

const app = new Koa();

app.use(mount('/hello', a));
app.use(mount('/world', b));

app.listen(3000);
console.log('listening on port 3000');

Try the following requests:

$ GET /
Not Found

$ GET /hello
Hello

$ GET /world
World

Mounting Middleware

Mount middleware at specific paths, allowing them to operate independently of the prefix, as they're not aware of it.

const mount = require('koa-mount');
const Koa = require('koa');

async function hello(ctx, next){
  await next();
  ctx.body = 'Hello';
}

async function world(ctx, next){
  await next();
  ctx.body = 'World';
}

const app = new Koa();

app.use(mount('/hello', hello));
app.use(mount('/world', world));

app.listen(3000);
console.log('listening on port 3000');

Optional Paths

The path argument is optional, defaulting to "/":

app.use(mount(a));
app.use(mount(b));

Debugging

Use the DEBUG environement variable to whitelist koa-mount debug output:

$ DEBUG=koa-mount node myapp.js &
$ GET /foo/bar/baz

  koa-mount enter /foo/bar/baz -> /bar/baz +2s
  koa-mount enter /bar/baz -> /baz +0ms
  koa-mount enter /baz -> / +0ms
  koa-mount leave /baz -> / +1ms
  koa-mount leave /bar/baz -> /baz +0ms
  koa-mount leave /foo/bar/baz -> /bar/baz +0ms

License

MIT

yjk2023@kbox/server@danielhuisman/koa-base@sprucelabs/spruce-skill-serverdanukoa2-middleware-slackerfrontity-core-recodedryoma_rssyoukuohao-accountsemil-backend@rappopo/cuk-httpegg-pandorassmart-api-booking-serviceaok-server@eviljs/std-rest@harmonizly/treehousevscode-test-webcase-managerebang-server-middleware@zrf9018/marvel-cli@zrf9018/marvel-mock-serveradminify.meyarsk@bybit/marvel-mock-serverxueyan-typescript-cliaf-bdnd-irs-invoices-api@infinitebrahmanuniverse/nolb-koa-mbackground_1.03drafterbitconcise-familykoa-rest-apikoa-koaddkoa-koamallkoa-koaweekkoa-koazkkoa-pbtrsshub@everything-registry/sub-chunk-2023json-validate-proxyday08-zm@daniellehuisman/koa-base@datacamp/mobile-teach@iguhealth/server@idio/core@igloo-be/core-admin@ibuild-community/applet-cli@imec/duxis-base@guivic/fabric-node@gswl/web@hap-toolkit-xiaomi/server@hap-toolkit/server@hap-toolkit-xiaomi/debugger@hap-toolkit/debugger@gutenye/fusion-cli@gerv/fusion-cli@geta6/coah@ditojs/servercorgiecorie@jayway/tds@mastermonar/fusion-clidbay-vogue@leo-guinan/koadocker-healthchecker-uidobi-mongofbcreate-koa-server@mitama/core@newbility/koa-core@netvarec/vapi-cli@netzo/api@neec/vapi-cli@neoblog/neoblog@neoblog/plugin-control-panel@nexys/koa-lib@key46/shopify-koa-app@khv/webz@khanghy3004/node-file-managerdanboard-proxy-serverd2l-nest-platform-koaday12-goodslistcrane-apicreevey@kastra/koa-next-app@k-suite/server@justathoughtnz/justathoughtnz-vapid-cli@lifeomic/content-admin-viewdd712@dollarshaveclub/react-runtime@dollarshaveclub/react-scripts@devim-front/server@devvit/dev-server@galaxar/server@fullstack-one/graphql@digest/koa@daytona/lds-editor@daytona/lds-server@daytona/lds-styleguide@daytona/lds-api@genx/server@makeflow/gateway-server
4.0.0

6 years ago

1.3.1

6 years ago

3.0.0

7 years ago

2.0.0

8 years ago

1.3.0

10 years ago

1.2.5

10 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

11 years ago

1.0.0

11 years ago