Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
koa-trie-router
Advanced tools
Trie routing for Koa based on routington.
Routes are orthogonal and strict, so the order of definition doesn't matter.
Unlike regexp routing, there's no wildcard routing and you can't next
to the next matching route.
See routington for more details.
1.x.x
versions of Trie-router2.x.x
versionsrouter.get
, router.put
, router.post
, etcOPTIONS
requests with allowed methods405 Method Not Allowed
support501 Not Implemented
supportconst Koa = require('koa')
const Router = require('trie-router')
let app = new Koa()
let router = new Router()
router
.use(function(ctx, next) {
console.log('* requests')
next()
})
.get(function(ctx, next) {
console.log('GET requests')
next()
})
.put('/foo', function (ctx) {
ctx.body = 'PUT /foo requests'
})
.post('/bar', function (ctx) {
ctx.body = 'POST /bar requests'
})
app.use(router.middleware())
app.listen(3000)
Handles all requests
router.use(function(ctx) {
ctx.body = 'test' // All requests
})
Handles requests only by one HTTP method
router.get(function(ctx) {
ctx.body = 'GET' // GET requests
})
Handles requests only by one HTTP method and one route
Where
paths
is {String|Array<String>}
middleware
is {Function|Array<Function>|AsyncFunction|Array<AsyncFunction>}
Signature
router
.get('/one', middleware)
.post(['/two','/three'], middleware)
.put(['/four'], [middleware, middleware])
.del('/five', middleware, middleware, middleware)
Like Express, all routes belong to a single middleware.
You can use koa-mount
for mounting of multiple routers:
const Koa = require('koa')
const mount = require('koa-mount')
const Router = require('trie-router')
let app = new Koa()
let router1 = new Router()
let router2 = new Router()
router1.get('/foo', middleware)
router2.get('/bar', middleware)
app.use(mount('/foo', router1.middleware()))
app.use(mount('/bar', router2.middleware()))
Checks if the server implements a particular method and returns true
or false
.
This is not middleware, so you would have to use it in your own middleware.
app.use(function(ctx, next) {
if (!router.isImplementedMethod(ctx.method)) {
ctx.status = 501
return
}
next()
})
ctx.params
will be defined with any matched parameters.
router.get('/user/:name', function (ctx, next) {
let name = ctx.params.name
let user = await User.get(name)
next()
})
The middleware throws an error with code
MALFORMEDURL when it encounters
a malformed path. An application can try/catch this upstream, identify the error
by its code, and handle it however the developer chooses in the context of the
application- for example, re-throw as a 404.
For path definitions, see routington.
FAQs
Trie-routing for Koa
The npm package koa-trie-router receives a total of 72 weekly downloads. As such, koa-trie-router popularity was classified as not popular.
We found that koa-trie-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 10 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.