All Projects → steambap → Koa Tree Router

steambap / Koa Tree Router

Licence: mit
high performance router for Koa

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Koa Tree Router

Koach Javascript
Production ready Koa2 boilerplate.
Stars: ✭ 79 (-18.56%)
Mutual labels:  koa
Koa Starter
🐨 A starter kit for a slightly opinionated koa project.
Stars: ✭ 87 (-10.31%)
Mutual labels:  koa
Smart Array To Tree
Convert large amounts of data array to tree fastly
Stars: ✭ 91 (-6.19%)
Mutual labels:  fast
Nuxt Stories
Nuxt stories module -- Painless (and now insanely fast) storybooking for Nuxt
Stars: ✭ 81 (-16.49%)
Mutual labels:  fast
Analys Middlewares
redux, koa, express 中间件对比实现分析
Stars: ✭ 83 (-14.43%)
Mutual labels:  koa
Award
⚙基于react的服务端渲染框架
Stars: ✭ 91 (-6.19%)
Mutual labels:  koa
Hugo Papermod
A fast, clean, responsive Hugo theme
Stars: ✭ 1,202 (+1139.18%)
Mutual labels:  fast
Fontend
使用Node、Vue、ElementUI、iViewUI,验证码等等搭建一个综合性网站(含后台管理系统)
Stars: ✭ 97 (+0%)
Mutual labels:  koa
Ofxfontstash
Easy (and fast) unicode string rendering addon for OpenFrameworks. FontStash is made by Andreas Krinke and Mikko Mononen
Stars: ✭ 84 (-13.4%)
Mutual labels:  fast
Cdnjs
🤖 CDN assets - The #1 free and open source CDN built to make life easier for developers.
Stars: ✭ 9,270 (+9456.7%)
Mutual labels:  fast
Liblognorm
a fast samples-based log normalization library
Stars: ✭ 81 (-16.49%)
Mutual labels:  fast
React Blog
react blog build with react hooks + koa2 + sequelize + mysql for personal usage.
Stars: ✭ 83 (-14.43%)
Mutual labels:  koa
Gracejs
A Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)
Stars: ✭ 1,302 (+1242.27%)
Mutual labels:  koa
Beebjit
A very fast BBC Micro emulator.
Stars: ✭ 81 (-16.49%)
Mutual labels:  fast
Naice Blog
😺 新的博客上线啦
Stars: ✭ 93 (-4.12%)
Mutual labels:  koa
Diary
📑 Zero-dependency, fast logging library for both Node and Browser.
Stars: ✭ 79 (-18.56%)
Mutual labels:  fast
Service Tools
Prepare your Node.js application for production
Stars: ✭ 89 (-8.25%)
Mutual labels:  koa
Vex Hugo
Vex is a product landing page theme/template created by Themefisher based on the latest Bootstrap 4 framework. It is fully responsive and beautifully crafted with Product Showcase, Testimonials, and Email Subscription sections
Stars: ✭ 97 (+0%)
Mutual labels:  fast
Libigl Python Bindings
IGL python bindings
Stars: ✭ 95 (-2.06%)
Mutual labels:  fast
Tabtoy
高性能表格数据导出器
Stars: ✭ 1,302 (+1242.27%)
Mutual labels:  fast

Koa tree router

Build Status npm npm downloads

Koa tree router is a high performance router for Koa.

Features

  • Fast. Up to 11 times faster than Koa-router. Benchmark

  • Express-style routing using router.get, router.put, router.post, etc.

  • Support for 405 method not allowed

  • Multiple middleware per route

How does it work?

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.

Installation

# npm
npm i koa-tree-router
# yarn
yarn add koa-tree-router

Usage

const Koa = require("koa");
const Router = require("koa-tree-router");

const app = new Koa();
const router = new Router();
router.get("/", function(ctx) {
  ctx.body = "hello, world";
});

app.use(router.routes());

app.listen(8080);

API

Router([options])

Instance a new router.
You can pass a middleware with the option onMethodNotAllowed.

const router = require('koa-tree-router')({
  onMethodNotAllowed(ctx){
    ctx.body = "not allowed"
  }
})

on(method, path, middleware)

Register a new route.

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

Shorthand methods

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)

routes

Returns router middleware.

app.use(router.routes());

nested routes

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

const Koa = require("koa");
const Router = require("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", function(ctx) {
  ctx.body = "hello, world";
});

app.use(router.routes());

app.listen(8080);

ctx.params

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

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

How to write routes

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

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].