# koa-mount

> Mounting middleware for koa

Latest version **4.2.0** (published 2025-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-mount
pnpm add koa-mount
yarn add koa-mount
bun add koa-mount
```

## Health

**Score 43/100 (D)** — status: stable.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.2.0 |
| Published | 2025-04-27 |
| First published | 2013-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/koa-mount) |
| Module format | CommonJS |
| Node | >= 7.6.0 |
| Dependencies | 2 |
| Unpacked size | 5.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 553 |
| Maintainers | coderhaoxin, aaron, juliangruber, eivifj, dead_horse, tjholowaychuk, jongleberry, fengmk2 |
| Keywords | koa, middleware, mount, mounting |

## Links

- npm: https://www.npmjs.com/package/koa-mount
- Repository: https://github.com/koajs/mount
- Homepage: https://github.com/koajs/mount#readme
- Issues: https://github.com/koajs/mount/issues
- npm.io page: https://npm.io/package/koa-mount

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.0.1
- [koa-compose](https://npm.io/package/koa-compose.md) ^4.1.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 4.2.0 (latest) — 2025-04-27
- 1.3.1 (latest-1) — 2018-09-12
- 2.0.0 (next) — 2016-03-07
- 4.1.0 — 2025-04-27
- 4.0.0 — 2018-09-26
- 3.0.0 — 2017-03-26
- 1.3.0 — 2014-05-08
- 1.2.5 — 2014-03-26
- 1.2.4 — 2014-03-26
- 1.2.3 — 2014-01-12
- 1.2.2 — 2013-12-22
- 1.2.1 — 2013-12-18
- 1.2.0 — 2013-11-29
- 1.1.0 — 2013-09-08
- 1.0.0 — 2013-08-17

## 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 **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

---
_Source: https://npm.io/package/koa-mount · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
