Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
fast-koa-router
Advanced tools
It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.
It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.
npm install fast-koa-router
const routes = {
get: {
'/path': async function(ctx, next) {
ctx.body = 'ok';
},
'/nested': {
'/path': async function(ctx, next) {},
'/path/:id': async function(ctx, next) {}
}
},
post: {
'/path': async function(ctx, next) {}
},
policy: {
'/path': async function(ctx, next) {},
'/nested': {
'/path': async function(ctx, next) {},
'/path/:id': async function(ctx, next) {}
}
},
prefix: {
'/': async function(ctx, next) {} // / matches all routes
}
};
// or
const routes = {
'/path': {
get: async function(ctx, next) {
ctx.body = 'ok';
},
post: async function(ctx, next) {},
policy: async function(ctx, next) {}
},
'/nested': {
'/path': { get: async function(ctx, next) {}, policy: async function(ctx, next) {} },
'/path/:id': { get: async function(ctx, next) {}, policy: async function(ctx, next) {} }
},
prefix: {
'/': async function(ctx, next) {} // / matches all routes
}
};
// or
const routes = {
get: {
'/path': async function(ctx, next) {},
'/nested/path': async function(ctx, next) {},
'/nested/path/:id': async function(ctx, next) {}
},
post: {
'/path': async function(ctx, next) {}
},
policy: {
'/path': async function(ctx, next) {},
'/nested/path': async function(ctx, next) {},
'/nested/path/:id': async function(ctx, next) {}
},
prefix: {
'/': async function(ctx, next) {} // / matches all routes
}
};
Supports
methods
const Koa = require('koa');
const app = new Koa();
const { router } = require('fast-koa-router');
app.use(router(routes));
app.listen(8080);
node
> const { router } = require('fast-koa-router');
> const route = router(routes);
> route.matching('/nested/path');
{
ctx: {
path: '/nested/path',
method: 'GET',
params: {},
_matchedRoute: '/nested/path'
},
middlewares: [
[AsyncFunction: '/nested/path'], // policy
[AsyncFunction: '/'], // prefix route
[AsyncFunction: '/nested/path'] // get route
]
}
> route.routes
// contains the compiled routes
Sometimes you need to have a fallback if no route matches with the requested url. You can have routes that end with a star eg:
const routes = {
get: {
'/path': async function(ctx, next) {},
'/path/subpath': async function(ctx, next) {},
'/path/*': async function(ctx, next) {
ctx.body='Nothing in /path matches this request';
ctx.status=404;
},
'/*': async function(ctx, next) {
ctx.body='Nothing here';
ctx.status=404;
}
}
Note that star symbol is only supported after version 1.1.0 and only when used in the end of a route.
There is no reason to use it in prefix routes. Prefix routes will always match get, post, delete, patch, put urls if they use the same prefix.
Policies are used to add authentication and authorization or any other kind of middleware. It is like all
and is executed before the matching route.
They must call next in order for the actual route to be executed.
Policies will be executed even if a matching get, post, put, delete, patch is not found
Policies are executed before anything else.
Prefixes are also used to add middleware. Unlike policies they will only be executed if a matching get, post, put, delete or patch is found. They are convenient to add authentication or authorization in many paths that start with a prefix eg: /api/v1
Prefixes are executed after policies and before other middleware. Note than both in prefix and policy middleware ctx.params and ctx._matchedRoute are available.
The path matching is pretty simple. Unlike other middlewares not all routes are checked so performance does not degrade with routes size. However complex regex matching is not supported.
Performances tests existing in this codebase and comparing fast-koa-router with @koa/router.
To start fast-koa-router example:
node performance-test/router/server.js
To start @koa/router example:
node performance-test/koa-router/server.js
Metrics have been taken using ab:
ab -k -n 1000000 -c 100 localhost:8080/api/v1/1/2
FAQs
It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.
The npm package fast-koa-router receives a total of 161 weekly downloads. As such, fast-koa-router popularity was classified as not popular.
We found that fast-koa-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.