Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
router for node.js / client
JavaScript Makefile
branch: master
Failed to load latest commit information.
lib Added missing condition in linkTo
src Rename NRouter to Pointer
test Added missing condition in linkTo
.eslintignore jshint -> eslint, + CS update
.eslintrc Perf opts
.gitignore Preapre real structure and draft API
.ndocrc Show internals in documentation
.npmignore Preapre real structure and draft API
.travis.yml Travis config update
HISTORY.md 0.3.1 released
LICENSE Preapre real structure and draft API
Makefile jshint -> eslint, + CS update
README.md Fixed badges links
index.js
package.json 0.3.1 released

README.md

Pointer

Build Status NPM version

Server/Client router:

API docs: http://nodeca.github.io/pointer/

npm install pointer

To make client version:

make browserify

You also need a modern browser or es5-shims to get it working.

API Overview

//
// fill in routes
//

// simple case
router.addRoute('/foo/{bar}', {
  params: {
    bar: /\d+/,
    type: 'integer' /* coerce result to int (string by default) */
  },
  meta: /* any data you want */
});

// with single parameter
router.addRoute({
  pattern: '/foo/{bar}',
  params: { bar: /\d+/ },
  meta: /* any data you want */
});

// route with optional param and it's default value
router.addRoute('/f{forum_id}(-{page}).html', {
  params: {
    forum_id: /\d+/
    page: {
      match: /\d+/
      default: 1
    }
  },
  meta: /* any data you want */
};

// named router (used for linkTo)
router.addRoute('/t{thread_id}', {
  name: 'thread.list',
  params: {
    thread_id: /\d+/
  },
  meta: /* any data you want */
});

// routes grouped by prefix
router.addRoute('/css/{file}(-{md5}).{ext}', {
  prefix: '/assets',
  meta: /* any data you want */
});
// -> /assets/css/{file}(-{md5}).{ext}


//
// Build links
//

router.linkTo('thread.list', {
  thread_id: 123
}); // -> '/t123'


//
// Build links with filling of missed parts.
//

router.linkTo('page', { id: 13 }) // -> '//domain.com/page/13'

router.linkTo('page', { id: 13 }, {
  protocol: 'http',
  hostname: 'defaultdomain.com',
  port:     3000
}) // -> 'http://domain.com:3000/page/13'


//
// find matching route
//

var match = router.match(url);
if (match) {
  match.params; // object with params, e.g. {id: 123, page: undefined}
  match.meta;   // your custom data
}


//
// Dump routes to "executable" string for client asset.
// Needed to keep regexps properly.
//

console.log(router.stringify());

var clientRouter = new Pointer(router.stringify());
Something went wrong with that request. Please try again.