1.0.1 โ€ข Published 3 years ago

koa-isomorphic-router v1.0.1

Weekly downloads
20
License
MIT
Repository
github
Last release
3 years ago

Koa Isomorphic Router


Build Status Coverage Status NPM version Code Size License PR's Welcome

The fastest elegant modern amiable Koa.js Router โšก.

Features

  • ๐Ÿฆ„ Based on top of Trek Router which inspired by Echo's Router.
  • ๐Ÿš€ Faster than other Koa.js router solutions.
  • ๐Ÿ’…๐Ÿป Express-style routing (app.get, app.post, app.put, app.delete, etc.)
  • ๐Ÿ”ฅ Blaze and lightweight router.
  • โš–๏ธ Tiny Bundle: less than 2.5kB (gzip)
  • ๐Ÿช Named URL parameters.
  • ๐ŸŽฏ Route middleware.
  • ๐Ÿฅž Support router layer middlewares.
  • ๐Ÿ“‹ Responds to OPTIONS requests with allowed methods.
  • โ›”๏ธ Support for 405 Method Not Allowed.
  • โŒ Support for 501 Path Not Implemented.
  • ๐Ÿงผ Support trailing slash and fixed path by automatic redirection.
  • โœจ Asynchronous support (async/await).
  • ๐ŸŽ‰ TypeScript support.

Note

Currently, this modules support 405 Method Not Allowed and 501 Path Not Implemented for 'static' routes only as you can see here.

As soon as possible when I get response here will support 'param' and 'match-any' routes.

For support RegExp in route path also I waiting for any response here.

Benchmarks

All Koa router solutions depend on path-to-regexp when our solution relies on the trek-router which has the best performance and not over the path-to-regexp only also on others such as (route-recognizer, route-trie, routington ...etc).

  • See trek-router benchmarks (trek-router better perf. than path-to-regexp).
  • See fastify benchmarks (koa-isomorphic-router better perf. than koa-router).

Installation

# npm
$ npm install koa-isomorphic-router
# yarn
$ yarn add koa-isomorphic-router

Usage

This is a practical example of how to use.

const Koa = require('koa')
const Router = require('koa-isomorphic-router')

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

router
  .get('/product/:id', (ctx, next) => {
    ctx.body = { productId: ctx.params.id }
  })

app.use(router.routes())

app.listen(5050)

API

new Router(options?)

Create a new router.

ParamTypeDescription
optionsObject
options.prefixStringprefix router paths
options.throwBooleanthrow error instead of setting status and header
options.notImplementedfunctionthrow the returned value in place of the default NotImplemented error
options.methodNotAllowedfunctionthrow the returned value in place of the default MethodNotAllowed error

router.get|post|put|patch|delete|all(path, ...middlewares)

The http methods provide the routing functionality in router.

Method middleware and handlers follow usual Koa middleware behavior, except they will only be called when the method and path match the request.

// handle a GET / request.
router.get('/', (ctx) => { ctx.body = 'Hello World!' })

router.prefix(prePath)

Route paths can be prefixed at the router level:

// handle a GET /prePath/users request.
router
  .prefix('/prePath')
  .get('/users', (ctx) => { ctx.body = 'Hello World!' })

router.route(path)

Lookup route with given path.

// handle a GET /users request.
router
  .route('/users')
  .get((ctx) => { ctx.body = 'Hello World!' })

router.use(...middlewares)

Use given middleware(s). Currently, use middleware(s) for all paths of router isntance.

router.routes()

Returns router middleware which handle a route matching the request.

Support

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

Call for Maintainers/Contributors

This module is a attempt to craft an isomorphic fast Router, from/to Koa.js community. So, don't hesitate to offer your help โค๏ธ.

Contributors

NameWebsite
Imed Jaberihttps://www.3imed-jaberi.com/

License


MIT ยฉ Imed Jaberi