Socket
Socket
Sign inDemoInstall

route-trie

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

route-trie

A trie-based URL router


Version published
Weekly downloads
1.5K
increased by48.88%
Maintainers
1
Weekly downloads
 
Created
Source

route-trie

A trie-based URL router.

NPM version Build Status

trie

Trie-based request routing

It is a different implementation from routington

route-trie is a trie-based URL router. Its goal is only to define and match URLs. It does not handle methods, headers, controllers, views, etc., in anyway. It is faster than traditional, linear, regular expression-matching routers, although insignficantly, and scales with the number of routes.

The purpose of this router isn't for performance, but to bring more structure to URL routing. The intention is for you to build a framework on top either in node.js or in the browser.

Implementations:

  • toa-router A trie router for toa(server).
  • hirouter HTML5 history and router, simple, powerful and no framework(browser).

Browser Support

IE9+

Demo

Installation

Node.js:

npm install route-trie

Bower:

bower install route-trie

API

var Trie = require('route-trie');

Trie([flagI])

flagI: Boolean, default false, ignore case.

var trie = new Trie();
var trie = new Trie(true); // ignore case for match

Trie.prototype.define(pattern)

var node = trie.define('/:type/:id([a-z0-9]{6})');
// assert(node._nodeState.pattern === '/:type/:id([a-z0-9]{6})');
// assert(node !== trie.define('/:type'));
// assert(node !== trie.define('/post'));
// assert(node === trie.define('/:type/:id([a-z0-9]{6})'));
// assert(trie.define('/:type') === trie.define('/:type1'));

The result node, will be an emtpy object, it has a private and not enumerable property _nodeState.

Each fragment of the pattern, delimited by a /, can have the following signature:

  • string - ex /post
  • string|string - | separated strings, ex /post|task
  • :name - Wildcard route matched to a name, ex /:type
  • (regex) - A regular expression match without saving the parameter (not recommended), ex /(post|task), /([a-z0-9]{6})
  • :name(regex)- Named regular expression match ex /:type/:id([a-z0-9]{6})
  • * - Match remaining path without saving the parameter (not recommended), ex /* will match all path.
  • :name(*)- Named regular expression match, match remaining path, ex /:type/:other(*) will match /post/x or /task/x/y or /any/x/y/z...

Trie.prototype.match(path)

var match = trie.match('/post');
// assert(match === null);

match = trie.match('/post/abc123');
// assert(match.node === trie.define('/:type/:id([a-z0-9]{6}'));
// assert.deepEqual(match.params, {type: 'post', id: 'abc123'})

The result match, unless null, will be an object with the following properties:

  • params - A list of named parameters, ex, match.params.id === 'abc123'.
  • node - The matched node.

Keywords

FAQs

Package last updated on 26 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc