A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/steambap/koa-tree-router below:

steambap/koa-tree-router: high performance router for Koa

Koa tree router is a high performance router for Koa.

The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree).

This module's tree implementation is based on julienschmidt/httprouter.

# npm
npm i koa-tree-router
# yarn
yarn add koa-tree-router
import Koa from "koa";
import Router from "koa-tree-router";

const app = new Koa();
const router = new Router();

router.get("/", (ctx) => {
  ctx.body = "hello, world";
});

app.use(router.routes());

app.listen(8080);

Instance a new router.

import Router from "koa-tree-router";

const router = new Router({
  onMethodNotAllowed(ctx) {
    ctx.body = "not allowed";
  },
  ignoreTrailingSlash: true
});
on(method, path, middleware)

Register a new route.

router.on('GET', '/example', (ctx) => {
  // your code
})

If you want to get expressive, here is what you can do:

router.get(path, middleware)
router.delete(path, middleware)
router.head(path, middleware)
router.patch(path, middleware)
router.post(path, middleware)
router.put(path, middleware)
router.options(path, middleware)
router.trace(path, middleware)
router.connect(path, middleware)

If you need a route that supports all methods you can use the all api.

router.all(path, middleware)

You can add middleware that is added to all future routes:

router.use(authMiddleware);
router.get("/foo", (ctx) => { /* your code */ });
router.get("/bar", (ctx) => { /* your code */ });
router.get("/baz", (ctx) => { /* your code */ });

This is equivalent to:

router.get("/foo", authMiddleware, (ctx) => { /* your code */ });
router.get("/bar", authMiddleware, (ctx) => { /* your code */ });
router.get("/baz", authMiddleware, (ctx) => { /* your code */ });

Caveat: use must be called before register a new handler. It does not append handlers to registered routes.

Returns router middleware.

app.use(router.routes());

A way to create groups of routes without incuring any per-request overhead.

import Koa from "koa";
import Router from "koa-tree-router";

const app = new Koa();
const router = new Router();
const group = router.newGroup("/foo");
// add a handler for /foo/bar
group.get("/bar", (ctx) => {
  ctx.body = "hello, world";
});

app.use(router.routes());

app.listen(8080);

Middleware added with use() are also added to the nested routes.

This object contains key-value pairs of named route parameters.

router.get("/user/:name", function() {
  // your code
});
// GET /user/1
ctx.params.name
// => "1"

There are 3 types of routes:

1.Static

Pattern: /static

 /static                   match
 /anything-else            no match

2.Named

Named parameters have the form :name and only match a single path segment:

Pattern: /user/:user

 /user/gordon              match
 /user/you                 match
 /user/gordon/profile      no match
 /user/                    no match

3.Catch-all

Catch-all parameters have the form *name and match everything. They must always be at the end of the pattern:

Pattern: /src/*filepath

 /src/                     match
 /src/somefile.go          match
 /src/subdir/somefile.go   match

This package has its own declaration files in NPM package, you don't have to do anything extra.

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4