Route matching and testing, nothing more than that. As simple as it could be.
Routable is a lightweight regular expression inspired HTTP route parser for Node.js. It only has one goal and that is to match and parse URL's for frameworks.
The module is released in the
npm registry as
npm install --save routable
--save automatically adds the routable module to your package.json.
All the examples in this getting started assume that you have included the
module in your code and exposed it as a
'use strict';var Routable = require'routable';
To create a new route simply construct a new
Routable instance with an URL
var foo = '/foo';
There are different patterns that can be used for testing against URL's. Routable supports testing against strings, Regular Expressions and even xRegExp based expressions. See patterns for more details.
Now that you've created your first
Routable instance you can use it to test
against URL's. To see if a URL matches the your
Routable instance you can use
Just like the
RegularExpression.test method, it returns a boolean indicating
if the given string matches the expression or not. The same is true for
Routable but instead of testing a Regular Express you're testing your pattern.
var foo = '/foo';footest'/bar'; // false;footest'/foo'; // true;footest'/fooo'; // false;
While quickly testing an URL is useful sometimes you also want to parse out the
information from the URL. If you have a capturing or named Regular Expression or
string you can use the
RegularExpression.exec you can either
undefined or an
with results as return value. With a
Routable instance you still get
undefined when there isn't a match but instead of an array you receive an
var foobar = '/foo/:bar';var res = foobarexec'/foo/foo';console.logresbar; // 'foo'
- Regular Expressions
- Capturing Regular Expressions
- Capturing strings
- Optional parameters
- Plain strings