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.
koa-tree-router
Advanced tools
Koa tree router is a high performance router for Koa.
Fast. Up to 12 times faster than Koa-router. Benchmark
Express routing using router.get
, router.put
, router.post
, etc.
Support for 405 method not allowed
Multiple middlewares per route
The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree).
This module's tree implementation is based on julienschmidt/httprouter.
const Koa = require("koa");
const Router = require("koa-tree-router");
const app = new Koa();
const router = new Router();
router.get("/", function(ctx) {
ctx.body = "hello, world";
});
app.use(router.routes());
app.listen(8080);
Instance a new router.
You can pass a middleware with the option onMethodNotAllowed
.
const router = require('koa-tree-router')({
onMethodNotAllowed(ctx){
ctx.body = "not allowed"
}
})
Register a new route.
router.on('GET', '/example', (ctx) => {
// your code
})
If you want tp get expressive, here is what you can do:
router.get(path, middleware)
router.delete(path, middleware)
router.head(path, middleware)
router.patch(path, middleware)
router.post(path, middleware)
router.put(path, middleware)
router.options(path, middleware)
router.trace(path, middleware)
router.connect(path, middleware)
If you need a route that supports all methods you can use the all
api.
router.all(path, middleware)
Returns router middleware.
app.use(router.routes());
This object contains key-value pairs of named route parameters.
router.get("/user/:name", function() {
// your code
});
// GET /user/1
ctx.params.name
// => "1"
There are 3 types of routes:
1.Static
Pattern: /static
/static match
/anything-else no match
2.Named
Named parameters have the form :name
and only match a single path segment:
Pattern: /user/:user
/user/gordon match
/user/you match
/user/gordon/profile no match
/user/ no match
3.Catch-all
Catch-all parameters have the form *name
and match everything. They must always be at the end of the pattern:
Pattern: /src/*filepath
/src/ match
/src/somefile.go match
/src/subdir/somefile.go match
FAQs
A high performance koa router
The npm package koa-tree-router receives a total of 1,253 weekly downloads. As such, koa-tree-router popularity was classified as popular.
We found that koa-tree-router demonstrated a not healthy version release cadence and project activity because the last version was released 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
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.