You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
$npminstallkoa-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.
constmount=require('koa-mount');constKoa=require('koa');// helloconsta=newKoa();a.use(asyncfunction(ctx,next){awaitnext();ctx.body='Hello';});// worldconstb=newKoa();b.use(asyncfunction(ctx,next){awaitnext();ctx.body='World';});// appconstapp=newKoa();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.
constmount=require('koa-mount');constKoa=require('koa');asyncfunctionhello(ctx,next){awaitnext();ctx.body='Hello';}asyncfunctionworld(ctx,next){awaitnext();ctx.body='World';}constapp=newKoa();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: