https://github.com/paveldeuce/router
Lightweight library to provide an API for handling routes
https://github.com/paveldeuce/router
algorithm algorithms constraints eslint-prettier jest jsdoc methods rest-api router routes tree tree-structure trie trie-tree
Last synced: 4 months ago
JSON representation
Lightweight library to provide an API for handling routes
- Host: GitHub
- URL: https://github.com/paveldeuce/router
- Owner: PavelDeuce
- Created: 2022-05-04T11:56:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-15T20:43:45.000Z (almost 3 years ago)
- Last Synced: 2025-01-04T12:17:53.331Z (5 months ago)
- Topics: algorithm, algorithms, constraints, eslint-prettier, jest, jsdoc, methods, rest-api, router, routes, tree, tree-structure, trie, trie-tree
- Language: JavaScript
- Homepage:
- Size: 118 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# router
[](https://github.com/PavelDeuce/router/actions/workflows/nodejs.yml)
[](https://github.com/PavelDeuce/js-algorithms-trees-project-lvl1/actions)
[](https://codeclimate.com/github/PavelDeuce/router/maintainability)
[](https://codeclimate.com/github/PavelDeuce/router/test_coverage)## About
Lightweight library to provide an API for handling routes
## Example
```javascript
import buildRouter from '@hexlet/code';const routes = [
{
path: '/courses/:course_id/exercises/:id',
constraints: { id: /\d+/, course_id: (courseId) => courseId.startsWith('js') },
handler: () => 'exercise!',
},
];const router = buildRouter(routes);
const result = router.serve({ path: '/courses/1/exercises/js' });
// { handler: [Function handler] , path: '/courses/1/exercises/js', params: { id: '1', course_id: 'js' }, method: 'GET' }
result.handler(result.params); // exercise!router.serve('/courses/noop/exercises/noop'); // Error: No such path
```