Mounting middleware for Koa.js applications that enables mounting apps and middleware at specific path prefixes
npx @tessl/cli install tessl/npm-koa-mount@4.2.0Koa Mount provides mounting middleware for Koa.js applications that enables mounting entire Koa applications or individual middleware functions at specific path prefixes. It strips the mount path from the URL temporarily until the stack unwinds, allowing mounted apps and middleware to function correctly regardless of which path segments they operate on.
npm install koa-mountconst mount = require('koa-mount');ES modules (if using a bundler that supports CommonJS imports):
import mount from 'koa-mount';const mount = require('koa-mount');
const Koa = require('koa');
// Create sub-applications
const blogApp = new Koa();
blogApp.use(async (ctx) => {
ctx.body = 'Blog content';
});
const apiApp = new Koa();
apiApp.use(async (ctx) => {
ctx.body = { message: 'API response' };
});
// Mount applications at different paths
const app = new Koa();
app.use(mount('/blog', blogApp));
app.use(mount('/api', apiApp));
app.listen(3000);The primary export that mounts Koa applications or middleware at specified path prefixes.
/**
* Mount app with prefix, app may be a Koa application or middleware function
* @param {String|Application|Function} prefix - Path prefix or app/middleware when path is omitted
* @param {Application|Function} [app] - Koa application, middleware function, or array of middleware
* @returns {Function} Koa middleware function
*/
function mount(prefix, app);Usage Patterns:
app.use(mount(koaApp));app.use(mount('/blog', blogApp));app.use(mount('/api', async (ctx, next) => {
ctx.body = 'API endpoint';
await next();
}));app.use(mount('/admin', [
authMiddleware,
adminMiddleware
]));const app = new Koa();
const blogApp = new Koa();
const adminApp = new Koa();
// Mount admin at /blog/admin
blogApp.use(mount('/admin', adminApp));
app.use(mount('/blog', blogApp));
// Request to /blog/admin/users will cascade through both mountsThe mount function implements precise path matching rules:
/prefix matches /prefix, /prefix/, /prefix/anything/prefix/ only matches paths with trailing slash after prefix/prefix does not match /prefixotherctx.path during middleware executionWhen mounting at non-root paths, the mount middleware modifies the Koa context:
interface KoaContext {
/** Original request path, temporarily modified during mounted middleware execution */
path: string;
/** Set to the mount prefix path during execution (only when not mounting at "/") */
mountPath?: string;
}Example of path transformation:
// Request to "/blog/posts/123" with mount prefix "/blog"
// Inside mounted middleware: ctx.path becomes "/posts/123"
// ctx.mountPath is set to "/blog"
// After middleware completes: ctx.path restored to "/blog/posts/123"The mount function includes validation and proper error handling:
Built-in debugging capabilities using the debug module:
DEBUG=koa-mount node app.jsDebug output includes:
Example debug output:
koa-mount mount /blog blogApp +0ms
koa-mount enter /blog/posts -> /posts +1ms
koa-mount leave /blog/posts -> /posts +5msinterface KoaApplication {
/** Array of middleware functions (used for mounting full applications) */
middleware: Function[];
/** Optional name for debugging purposes */
name?: string;
}/**
* Standard Koa middleware function signature
* @param {Object} ctx - Koa context object
* @param {Function} next - Next middleware function
*/
type MiddlewareFunction = (ctx: Object, next: Function) => Promise<void>;The mount function relies on these external dependencies: