z-router

method and pathname router for node http server.


License
ISC
Install
npm install z-router@0.0.1

Documentation

z-router

Most of web applications need to resolve a pair of method (GET, POST or...) and pathname (starts with /) into controller path and action name with params. (like the Ruby on Rails!)

For node http server, then, this "z-router" resolve them simply. ;)

Installation

$ npm install z-router

Usage

Initialize

var ZRouter = require('z-router');
var router = ZRouter(routes, options);

options:

  • ctrlDir
    • controllers directory path. default: './controllers'

routes:

var ns = ZRouter.namespace;
var routes = ns('root', {
  '': 'index',
  'info': 'info',
  'contact': {GET: 'form', POST: 'contact'}
}, [
  ns('articles', {
    '': 'index',
    ':year/:month/:day/:number': 'show'
  }),
  ns('api', [
    ns('v1', [
      ns('users', {
        '': {GET: 'index', POST: 'create'},
        ':id': [{GET: 'show', PUT: 'update', DELETE: 'delete'},
          ns('hobbies', {
            '': {GET: 'index', POST: 'create'},
            ':hobbyId': {DELETE: 'delete'}
          })
        ],
        ':id/bar': 'bar'
      })
    ])
  ])
]);

ZRouter.namespace(name [, actions] [, children])

  • name <String>
  • actions <Array[String/Object[path:name]/namespaceObj]> (option)
    • alse ok as not array
  • children <Array[namespace]> (option)

Make controller modules

- /
  |- package.json
  `- controllers/
     |- root.js
     |- articles.js
     `- api/
        `- v1/
           |- users.js
           `- users
              `- hobbies.js

ex) root.js:

exports.index = function(request, response, params) {
  response.write('called: root#index');
};
exports.info = function(request, response, params) {
  response.write('called: root#info');
};
exports.form = function(request, response, params) {
  response.write('called: root#form');
};
exports.contact = function(request, response, params) {
  response.write('called: root#contact');
};

Routing

var route = router.route('GET', '/info');
if( route && typeof route.controller == 'function' ) {
  route.controller(request, response, route.params);
}

route object has these properties:

  • method ('GET', 'POST'...)
  • pathname ('/', '/info', '/api/v1/users/:id/edit'...)
  • ctrlPath
    • / or /info or /contact -> '/root'
    • /articles or /articles/2000/10/15/2 -> '/articles'
    • /api/v1/users or /api/v1/users/123 or... -> '/api/v1/users'
  • actionName
    • GET / -> 'index'
    • GET /contact -> 'form'
    • POST /contact -> 'contact'
    • PUT /api/v1/users/123 -> 'update'
  • controller
    • controller function
  • params
    • /articles/2000/10/15/2 -> {year: '2000', month: '10', day: '15', number: '2'}
    • /api/v1/users/123 -> {id: '123'}
    • /api/v1/users/123/hobbies/foobar -> {id: '123', hobbyId: 'foobar'}

Utils

check routes:

console.log(router.routesToString());

Links