https://github.com/daidr/koa-router-dynamic
A patch for @koa/router to search for or remove routes without restarting the process.
https://github.com/daidr/koa-router-dynamic
koa koa-router koa2
Last synced: 4 months ago
JSON representation
A patch for @koa/router to search for or remove routes without restarting the process.
- Host: GitHub
- URL: https://github.com/daidr/koa-router-dynamic
- Owner: daidr
- License: mit
- Created: 2021-10-18T09:36:09.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-19T14:04:40.000Z (over 4 years ago)
- Last Synced: 2025-10-13T15:54:29.720Z (8 months ago)
- Topics: koa, koa-router, koa2
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/koa-router-dynamic
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [koa-router-dynamic](https://github.com/daidr/koa-router-dynamic)
> A patch for [@koa/router](https://github.com/koajs/router).
[](https://npmjs.org/package/koa-router-dynamic)
[](https://npmjs.org/package/koa-router-dynamic)
[](http://nodejs.org/download/)
* Search routes by path(s)
* Search routes by custom handler
* Remove routes without restarting the process
## Installation
```bash
# npm ..
npm i koa-router-dynamic
# yarn ..
yarn add koa-router-dynamic
```
## Usage
### router.searchRoutesByPath(path) ⇒ Array.<Layer>
Search routes in the stack by path(s).
**Kind**: instance method of Router
| Param | Type | Description |
|-------|----------------------------------------------------------|---------------------|
| path | String \| Array.<String> | Path string(Array). |
**Example**
```javascript
const Koa = require('koa');
const Router = require('@koa/router');
require('koa-router-dynamic');
const app = new Koa();
const router = new Router();
router.get("/example/:id", ctx => {
ctx.body = "test";
})
app.use(router.routes());
console.log(router.searchRoutesByPath("/example/:id"));
```
### router.searchRoutes(handler) ⇒ Array.<Layer>
Search routes in the stack by custom handler.
**Kind**: instance method of Router
| Param | Type | Description |
|---------|-----------------------|-------------------|
| handler | function | Handler function. |
**Example**
```javascript
const Koa = require('koa');
const Router = require('@koa/router');
require('koa-router-dynamic');
const app = new Koa();
const router = new Router();
router.get("/example/:id", ctx => {
ctx.body = "test";
})
app.use(router.routes());
console.log(router.searchRoutes(layer => layer.path === "/example/:id"));
```
### router.removeAllRoutes() ⇒ Router
Remove all routes in the stack.
**Kind**: instance method of Router
**Example**
```javascript
const Koa = require('koa');
const Router = require('@koa/router');
require('koa-router-dynamic');
const app = new Koa();
const router = new Router();
router.get("/example/:id", ctx => {
ctx.body = "test";
})
app.use(router.routes());
router.removeAllRoutes();
```
### router.removeRoutes(layers) ⇒ Router
Remove routes in the stack.
**Kind**: instance method of Router
| Param | Type | Description |
|--------|--------------------------------------------------------|-------------------|
| layers | Layer \| Array.<Layer> | layers to remove. |
**Example**
```javascript
const Koa = require('koa');
const Router = require('@koa/router');
require('koa-router-dynamic');
const app = new Koa();
const router = new Router();
router.get("/example/:id", ctx => {
ctx.body = "test";
})
app.use(router.routes());
let routesToRemove = router.searchRoutes(layer => layer.path === "/example/:id");
router.removeRoutes(routesToRemove);
```
## Contributing
Please submit all issues and pull requests to the [daidr/koa-router-dynamic](http://github.com/daidr/koa-router-dynamic) repository!
## Support
If you have any problem or suggestion please open an issue [here](https://github.com/daidr/koa-router-dynamic/issues).
## License
[MIT](LICENSE)