Git Product home page Git Product logo

nrouter's Introduction

nRouter

a objective router middleware for koa/express

install

npm i nrouter

usage

nRouter convert nested object to a flat route table, like this:

{
  a: fn,
  b: {c: fn},
  "d/e": fn,
  "user/:id": fn
}
----to----
/a ==> fn
/b/c ==> fn
/d/e ==> fn
/user/xxxx ==> fn(msg param contains id:'xxxx')

All matched request will all route to one function, so if you want make it RESTful, you need to add condition on this function.

for koa

const nRouter =  require('nrouter').koa;
const Koa = require('koa');
const app = new Koa();

app.use(
  nRouter({hello})
);

function hello(msg,headers,ctx,next) {
  return 'world';
}

app.listen(9000);

API

nRouter(obj, resolveFn, rejectFn)

obj

The route object, can be nested, can contains route parameters like express, final

resolveFn

Wrap the route function, you can defined global output format on here

rejectFn

If your route function throw error, it will throw to rejectFn, you can defined global error format on here

nrouter's People

Contributors

ngtmuzi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.