koa-router-dynamic
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

koa-router-dynamic

A patch for @koa/router.

NPM version NPM Downloads Node.js Version

  • Search routes by path(s)
  • Search routes by custom handler
  • Remove routes without restarting the process

Installation

# 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

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

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

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

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 repository!

Support

If you have any problem or suggestion please open an issue here.

License

MIT

Package Sidebar

Install

npm i koa-router-dynamic

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

13 kB

Total Files

6

Last publish

Collaborators

  • daidr