Gathering detailed insights and metrics for @eggjs/router
Gathering detailed insights and metrics for @eggjs/router
Gathering detailed insights and metrics for @eggjs/router
Gathering detailed insights and metrics for @eggjs/router
router for eggjs, fork from koa-router with some additional features
npm install @eggjs/router
Typescript
Module System
Min. Node Version
Node Version
NPM Version
93.2
Supply Chain
98.1
Quality
85.5
Maintenance
100
Vulnerability
100
License
TypeScript (98.3%)
JavaScript (1.08%)
Shell (0.34%)
Makefile (0.27%)
Total Downloads
8,326,975
Last Day
4,091
Last Week
21,152
Last Month
90,447
Last Year
1,096,079
51 Stars
419 Commits
7 Forks
23 Watching
4 Branches
67 Contributors
Latest Version
3.0.5
Package Id
@eggjs/router@3.0.5
Unpacked Size
314.11 kB
Size
50.35 kB
File Count
30
NPM Version
10.7.0
Node Version
18.20.3
Publised On
16 Jun 2024
Cumulative downloads
Total Downloads
Last day
12.6%
4,091
Compared to previous day
Last week
2.2%
21,152
Compared to previous week
Last month
-5.7%
90,447
Compared to previous month
Last year
-54%
1,096,079
Compared to previous year
Router core component for Egg.js.
This repository is a fork of koa-router. with some additional features. And thanks for the great work of @alexmingoia and the original team.
Create a new router.
Param | Type | Description |
---|---|---|
[opts] | Object | |
[opts.prefix] | String | prefix router paths |
Example Basic usage:
1import Koa from '@eggjs/koa'; 2import Router from '@eggjs/router'; 3 4const app = new Koa(); 5const router = new Router(); 6 7router.get('/', async (ctx, next) => { 8 // ctx.router available 9}); 10 11app 12 .use(router.routes()) 13 .use(router.allowedMethods());
Router
Create router.verb()
methods, where verb is one of the HTTP verbs such
as router.get()
or router.post()
.
Match URL patterns to callback functions or controller actions using router.verb()
,
where verb is one of the HTTP verbs such as router.get()
or router.post()
.
Additionaly, router.all()
can be used to match against all methods.
1router 2 .get('/', (ctx, next) => { 3 ctx.body = 'Hello World!'; 4 }) 5 .post('/users', (ctx, next) => { 6 // ... 7 }) 8 .put('/users/:id', (ctx, next) => { 9 // ... 10 }) 11 .del('/users/:id', (ctx, next) => { 12 // ... 13 }) 14 .all('/users/:id', (ctx, next) => { 15 // ... 16 });
When a route is matched, its path is available at ctx.routePath
and if named,
the name is available at ctx.routeName
Route paths will be translated to regular expressions using path-to-regexp.
Query strings will not be considered when matching requests.
Routes can optionally have names. This allows generation of URLs and easy renaming of URLs during development.
1router.get('user', '/users/:id', (ctx, next) => { 2 // ... 3}); 4 5router.url('user', 3); 6// => "/users/3"
Multiple middleware may be given:
1router.get( 2 '/users/:id', 3 (ctx, next) => { 4 return User.findOne(ctx.params.id).then(function(user) { 5 ctx.user = user; 6 next(); 7 }); 8 }, 9 ctx => { 10 console.log(ctx.user); 11 // => { id: 17, name: "Alex" } 12 } 13);
Nesting routers is supported:
1const forums = new Router(); 2const posts = new Router(); 3 4posts.get('/', (ctx, next) => {...}); 5posts.get('/:pid', (ctx, next) => {...}); 6forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods()); 7 8// responds to "/forums/123/posts" and "/forums/123/posts/123" 9app.use(forums.routes());
Route paths can be prefixed at the router level:
1const router = new Router({ 2 prefix: '/users' 3}); 4 5router.get('/', ...); // responds to "/users" 6router.get('/:id', ...); // responds to "/users/:id"
Named route parameters are captured and added to ctx.params
.
1router.get('/:category/:title', (ctx, next) => { 2 console.log(ctx.params); 3 // => { category: 'programming', title: 'how-to-node' } 4});
The path-to-regexp module is used to convert paths to regular expressions.
Kind: instance property of Router
Param | Type | Description |
---|---|---|
path | String | |
[middleware] | function | route middleware(s) |
callback | function | route callback |
function
Returns router middleware which dispatches a route matching the request.
Kind: instance property of Router
Router
Use given middleware.
Middleware run in the order they are defined by .use()
. They are invoked
sequentially, requests start at the first middleware and work their way
"down" the middleware stack.
Kind: instance method of Router
Param | Type |
---|---|
[path] | String |
middleware | function |
[...] | function |
Example
1// session middleware will run before authorize 2router 3 .use(session()) 4 .use(authorize()); 5 6// use middleware only with given path 7router.use('/users', userAuth()); 8 9// or with an array of paths 10router.use(['/users', '/admin'], userAuth()); 11 12app.use(router.routes());
Router
Set the path prefix for a Router instance that was already initialized.
Kind: instance method of Router
Param | Type |
---|---|
prefix | String |
Example
1router.prefix('/things/:thing_id')
function
Returns separate middleware for responding to OPTIONS
requests with
an Allow
header containing the allowed methods, as well as responding
with 405 Method Not Allowed
and 501 Not Implemented
as appropriate.
Kind: instance method of Router
Param | Type | Description |
---|---|---|
[options] | Object | |
[options.throw] | Boolean | throw error instead of setting status and header |
[options.notImplemented] | function | throw the returned value in place of the default NotImplemented error |
[options.methodNotAllowed] | function | throw the returned value in place of the default MethodNotAllowed error |
Example
1import Koa from '@eggjs/koa'; 2import Router from '@eggjs/router'; 3 4const app = new Koa(); 5const router = new Router(); 6 7app.use(router.routes()); 8app.use(router.allowedMethods());
Example with Boom
1import Koa from '@eggjs/koa'; 2import Router from '@eggjs/router'; 3import Boom from 'boom'; 4 5const app = new Koa(); 6const router = new Router(); 7 8app.use(router.routes()); 9app.use(router.allowedMethods({ 10 throw: true, 11 notImplemented: () => new Boom.notImplemented(), 12 methodNotAllowed: () => new Boom.methodNotAllowed() 13}));
Router
Redirect source
to destination
URL with optional 30x status code
.
Both source
and destination
can be route names.
1router.redirect('/login', 'sign-in');
This is equivalent to:
1router.all('/login', ctx => { 2 ctx.redirect('/sign-in'); 3 ctx.status = 301; 4});
Kind: instance method of Router
Param | Type | Description |
---|---|---|
source | String | URL or route name. |
destination | String | URL or route name. |
[code] | Number | HTTP status code (default: 301). |
Layer
| false
Lookup route with given name
.
Kind: instance method of Router
Param | Type |
---|---|
name | String |
String
| Error
Generate URL for route. Takes a route name and map of named params
.
Kind: instance method of Router
Param | Type | Description |
---|---|---|
name | String | route name |
params | Object | url parameters |
[options] | Object | options parameter |
[options.query] | Object | String | query options |
Example
1router.get('user', '/users/:id', (ctx, next) => { 2 // ... 3}); 4 5router.url('user', 3); 6// => "/users/3" 7 8router.url('user', { id: 3 }); 9// => "/users/3" 10 11router.use((ctx, next) => { 12 // redirect to named route 13 ctx.redirect(ctx.router.url('sign-in')); 14}) 15 16router.url('user', { id: 3 }, { query: { limit: 1 } }); 17// => "/users/3?limit=1" 18 19router.url('user', { id: 3 }, { query: "limit=1" }); 20// => "/users/3?limit=1"
Router
Run middleware for named route parameters. Useful for auto-loading or validation.
Kind: instance method of Router
Param | Type |
---|---|
param | String |
middleware | function |
Example
1router 2 .param('user', (id, ctx, next) => { 3 ctx.user = users[id]; 4 if (!ctx.user) return ctx.status = 404; 5 return next(); 6 }) 7 .get('/users/:user', ctx => { 8 ctx.body = ctx.user; 9 }) 10 .get('/users/:user/friends', ctx => { 11 return ctx.user.getFriends().then(function(friends) { 12 ctx.body = friends; 13 }); 14 }) 15 // /users/3 => {"id": 3, "name": "Alex"} 16 // /users/3/friends => [{"id": 4, "name": "TJ"}]
String
Generate URL from url pattern and given params
.
Kind: static method of Router
Param | Type | Description |
---|---|---|
path | String | url pattern |
params | Object | url parameters |
[options] | Object | options parameter |
[options.query] | Object | String | query options |
Example
1const url = Router.url('/users/:id', {id: 1}); 2// => "/users/1" 3 4const url = Router.url('/users/:id', {id: 1}, {query: { active: true }}); 5// => "/users/1?active=true"
Run tests using npm test
.
This project follows the git-contributor spec, auto updated at Sun Jun 16 2024 12:28:11 GMT+0800
.
No vulnerabilities found.
No security vulnerabilities found.