Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/mount
Mount other Koa applications or middleware to a given pathname
https://github.com/koajs/mount
Last synced: 7 days ago
JSON representation
Mount other Koa applications or middleware to a given pathname
- Host: GitHub
- URL: https://github.com/koajs/mount
- Owner: koajs
- Created: 2013-08-17T06:48:38.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T03:01:13.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T15:22:57.315Z (about 1 month ago)
- Language: JavaScript
- Size: 55.7 KB
- Stars: 553
- Watchers: 8
- Forks: 51
- Open Issues: 21
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - mount - Mount other Koa applications or middleware to a given pathname (Middleware)
- awesome-koa - koa-mount - 挂载Koa应用作为中间件。 ![](https://img.shields.io/github/stars/koajs/mount.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-mount.svg?style=flat-square) (仓库 / 中间件)
README
# 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/bazkoa-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