Gathering detailed insights and metrics for moa-router
Gathering detailed insights and metrics for moa-router
Gathering detailed insights and metrics for moa-router
Gathering detailed insights and metrics for moa-router
npm install moa-router
Typescript
Module System
Node Version
NPM Version
57.9
Supply Chain
98.6
Quality
79
Maintenance
50
Vulnerability
100
License
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
0%
3
Compared to previous day
Last week
100%
4
Compared to previous week
Last month
-45.5%
12
Compared to previous month
Last year
-31.6%
214
Compared to previous year
a better && faster router base on find-my-way(support koa/express/http)
A crazy fast HTTP router, internally uses an highly performant Radix Tree (aka compact Prefix Tree), supports route params, wildcards, and it's framework independent.
If you want to see a benchmark comparison with the most commonly used routers, see here.
Do you need a real-world example that uses this router? Check out Fastify.
$ autocannon 127.0.0.1:3000/test
QPS
$ npm i --save moa-router
Koa
1const http = require('http') 2const Koa = require('koa'); 3const app = new Koa(); 4 5const router = require('moa-router')() 6 7router.get('/', (ctx, next) => { 8 ctx.body = {'path': 'root'} 9}) 10 11router.on('GET', '/test', (ctx, next) => { 12 ctx.body = {'hello': 'world'} 13}) 14 15app.use(router.routes()); 16 17app.use(async function (ctx, next) { 18 ctx.body = "default" 19}); 20 21const server = http.createServer(app.callback()) 22 23server.listen(3030, err => { 24 if (err) throw err 25 console.log('Server listening on: http://localhost:3000') 26})
Express
1const http = require('http') 2const express = require('express') 3const app = express() 4 5const router = require('moa-router')() 6router.type = 'express' 7 8router.get('/', (req, res, next) => { 9 res.json({'path': 'root'}) 10}) 11 12router.on('GET', '/test', (req, res, next) => { 13 res.json({'hello': 'world'}) 14}) 15 16app.use(router.routes()) 17 18app.use(async function (ctx, next) { 19 res.send("default") 20}) 21 22const server = http.createServer(app) 23 24server.listen(3000, err => { 25 if (err) throw err 26 console.log('Server listening on: http://localhost:3000') 27})
HTTP(fastify)
1'use strict' 2 3const http = require('http') 4const router = require('moa-router')() 5router.type = 'http' 6 7router.on('GET', '/test', (req, res, params) => { 8 res.end('{"hello":"world"}') 9}) 10 11const server = http.createServer(router.routes()) 12 13server.listen(3000, err => { 14 if (err) throw err 15 console.log('Server listening on: http://localhost:3000') 16})
Instance a new router.
You can pass a default route with the option defaultRoute
.
1const router = require('moa-router')({ 2 defaultRoute: (ctx, next) => { 3 ctx.status = 404 4 } 5})
Register a new route.
1router.on('GET', '/example', (ctx, next) => { 2 // your koa code 3})
Last argument, store
is used to pass an object that you can access later inside the handler function. If needed, store
can be updated.
1router.on('GET', '/example', (ctx, next) => { 2 assert.equal(ctx.store, { message: 'hello world' }) 3}, { message: 'hello world' })
Register a new route for each method specified in the methods
array.
It comes handy when you need to declare multiple routes with the same handler but different methods.
1router.on(['GET', 'POST'], '/example', (ctx, next) => { 2 // your code 3})
To register a parametric path, use the colon before the parameter name. For wildcard use the star. Remember that static routes are always inserted before parametric and wildcard.
1// parametric 2router.on('GET', '/example/:userId', (ctx, next) => {})) 3router.on('GET', '/example/:userId/:secretToken', (ctx, next) => {})) 4 5// wildcard 6router.on('GET', '/example/*', (ctx, next) => {}))
Regular expression routes are supported as well, but pay attention, RegExp are very expensive in term of performance!
1// parametric with regexp 2router.on('GET', '/example/:file(^\\d+).png', () => {}))
It's possible to define more than one parameter within the same couple of slash ("/"). Such as:
1router.on('GET', '/example/near/:lat-:lng/radius/:r', (ctx, next) => {}))
Remember in this case to use the dash ("-") as parameters separator.
Finally it's possible to have multiple parameters with RegExp.
1router.on('GET', '/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (ctx, next) => {}))
In this case as parameter separator it's possible to use whatever character is not matched by the regular expression.
Having a route with multiple parameters may affect negatively the performance, so prefer single parameter approach whenever possible, especially on routes which are on the hot path of your application.
The routes are matched in the following order:
static
parametric
wildcards
parametric(regex)
multi parametric(regex)
1const assert = require('assert') 2const router = require('moa-router')({ 3 defaultRoute: (ctx, next) => { 4 assert(ctx.req.url === '/example/shared/nested/oops') 5 } 6}) 7 8router.on('GET', '/example/shared/nested/test', (ctx, next) => { 9 assert.fail('We should not be here') 10}) 11 12router.on('GET', '/example/:param/nested/oops', (ctx, next) => { 13 assert.fail('We should not be here') 14}) 15 16router.lookup({ method: 'GET', url: '/example/shared/nested/oops' }, null)
1const findMyWay = FindMyWay({ 2 defaultRoute: (ctx, next) => {} 3}) 4 5findMyWay.on('GET', '/user/:userId(^\\d+)', (ctx, next) => {}) 6 7findMyWay.on('GET', '/user/:username(^[a-z]+)', (ctx, next) => {}) 8// Method 'GET' already declared for route ':'
If you want an even nicer api, you can also use the shorthand methods to declare your routes.
1router.get(path, handler [, store]) 2router.delete(path, handler [, store]) 3router.head(path, handler [, store]) 4router.patch(path, handler [, store]) 5router.post(path, handler [, store]) 6router.put(path, handler [, store]) 7router.options(path, handler [, store]) 8router.trace(path, handler [, store]) 9router.connect(path, handler [, store])
If you need a route that supports all methods you can use the all
api.
1router.all(path, handler [, store])
Start a new search, ctx
and next
are the server ctx.req/ctx.res objects.
If a route is found it will automatically called the handler, otherwise the default route will be called.
The url is sanitized internally, all the parameters and wildcards are decoded automatically.
1router.lookup(ctx, next)
Return (if present) the route registered in method:path.
The path must be sanitized, all the parameters and wildcards are decoded automatically.
1router.find('GET', '/example') 2// => { handler: Function, params: Object, store: Object} 3// => null
Prints the representation of the internal radix tree, useful for debugging.
1findMyWay.on('GET', '/test', () => {}) 2findMyWay.on('GET', '/test/hello', () => {}) 3findMyWay.on('GET', '/hello/world', () => {}) 4 5console.log(findMyWay.prettyPrint()) 6// └── / 7// ├── test (GET) 8// │ └── /hello (GET) 9// └── hello/world (GET)
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)i5ting
i5ting@126.com如有建议或意见,请在issue提问或邮件
find-my-way - MIT
trekjs/router - MIT
this repo is released under the MIT License. Copyright © 2017 i5ting
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More