koajs/mount
关于
将其他 Koa 应用或中间件挂载到指定路径
其他项目实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文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 NODE_DEBUG environment variable to whitelist koa-mount debug output:
$ NODE_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
探索相关项目
加载中…