diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/koa-mount/Readme.md | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/koa-mount/Readme.md')
| -rw-r--r-- | node_modules/koa-mount/Readme.md | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/node_modules/koa-mount/Readme.md b/node_modules/koa-mount/Readme.md new file mode 100644 index 00000000..3d22b5f8 --- /dev/null +++ b/node_modules/koa-mount/Readme.md @@ -0,0 +1,127 @@ +# 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 + +```js +$ npm install koa-mount +``` + +## Examples + + View the [./examples](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. + +```js +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. + +```js +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 "/": + +```js +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 |
