koa-mount

Mounts a Koa application or middleware at a path prefix, stripping the prefix from `ctx.url` for the mounted app.

Since Koa 2 Spec ↗

Syntax

import mount from 'koa-mount';
app.use(mount(path, app2orMiddleware));

Parameters

NameTypeRequiredDescription
path string Yes The URL path prefix to mount at (e.g. `"/api"`). Must start with `/`.
app2orMiddleware Application | AsyncFunction Yes A Koa `Application` instance or a single middleware function to mount at the given path.

Returns

function — A Koa middleware that delegates matching requests to the mounted app/middleware.

Examples

import Koa from 'koa';
import mount from 'koa-mount';

const app = new Koa();

// Sub-application for /api
const api = new Koa();
api.use(async (ctx) => {
  ctx.body = { version: 'v1', path: ctx.url }; // ctx.url is /users, not /api/users
});

app.use(mount('/api', api));

app.use(async (ctx) => {
  ctx.body = 'root app';
});

app.listen(3000);
Output
GET /api/users → {"version":"v1","path":"/users"}
GET /          → "root app"

Notes

`koa-mount` strips the mount prefix from `ctx.url` inside the mounted app so it can be developed independently. The original URL (including the prefix) is still available via `ctx.originalUrl`. Useful for composing multiple Koa apps (e.g. `v1` and `v2` APIs) in a single process.

See also