Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
The `koa-mount` package is a middleware for the Koa framework that allows you to mount other Koa applications or middleware at specific paths. This is useful for structuring your application into smaller, more manageable pieces or for integrating third-party applications.
Mounting a Koa application at a specific path
This feature allows you to mount a sub-application at a specific path. In this example, requests to `/sub` will be handled by `subApp`.
const Koa = require('koa');
const mount = require('koa-mount');
const app = new Koa();
const subApp = new Koa();
subApp.use(async (ctx) => {
ctx.body = 'Hello from subApp!';
});
app.use(mount('/sub', subApp));
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Mounting middleware at a specific path
This feature allows you to mount specific middleware at a given path. In this example, requests to `/middleware` will be handled by the `middleware` function.
const Koa = require('koa');
const mount = require('koa-mount');
const app = new Koa();
const middleware = async (ctx, next) => {
ctx.body = 'Hello from middleware!';
await next();
};
app.use(mount('/middleware', middleware));
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
The `koa-router` package provides a powerful routing solution for Koa applications. It allows you to define routes and their handlers, including support for nested routes and middleware. Unlike `koa-mount`, which is focused on mounting entire applications or middleware at specific paths, `koa-router` is more focused on defining and handling individual routes.
While not a Koa-specific package, `express` is a popular web framework for Node.js that includes built-in support for routing and middleware. It offers similar functionality to `koa-mount` in terms of mounting sub-applications and middleware, but it is designed for use with the Express framework rather than Koa.
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.
$ npm install koa-mount
View the ./examples directory for working examples.
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.
var mount = require('koa-mount');
var koa = require('koa');
// hello
var a = koa();
a.use(function *(next){
yield next;
this.body = 'Hello';
});
// world
var b = koa();
b.use(function *(next){
yield next;
this.body = 'World';
});
// app
var app = 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
Mount middleware at specific paths, allowing them to operate independently of the prefix, as they're not aware of it.
var mount = require('koa-mount');
var koa = require('koa');
function *hello(next){
yield next;
this.body = 'Hello';
}
function *world(next){
yield next;
this.body = 'World';
}
var app = koa();
app.use(mount('/hello', hello));
app.use(mount('/world', world));
app.listen(3000);
console.log('listening on port 3000');
The path argument is optional, defaulting to "/":
app.use(mount(a));
app.use(mount(b));
Use the DEBUG environement variable to whitelist koa-mount debug output:
$ 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
MIT
FAQs
Mounting middleware for koa
We found that koa-mount demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.