Shrinkroute
Named and nested routes for Express 3+, with URL building support. Helps you in achieving DRY routes!
Easy as that:
var shrinkr = ; // in your routes... { User;} // or views...<a href="<%= url( "user", { id: 1 }) %>">User profile</a><a href="<%= fullUrl( "user", { id: 1 }) %>">User profile</a>
ATTENTION: Version 0.3.0 automatically adds the middleware responsible for providing URL builders in the view. When upgrading from previous versions, remove app.use( shrinkr.middleware );
line.
Nested routes
Nested routes are separated by an character, which is by default .
(you may customize it if you want). When you set nested routes, they'll inherit their parent's route.
For example:
;
This will end up in a route named admin
which map to /admin
, and another route named admin.users
which map to /admin/users
.
URL Builders in the view and in the request
The following functions are automatically available to you in every route set by Shrinkroute:
req.buildUrl
andres.locals.url
- builds paths for a route. The same as usingshrinkr.url()
.req.buildFullUrl
andres.locals.fullUrl
- builds full URLs for a route. The same as usingshrinkr.fullUrl()
.
Installation
Install Shrinkroute via NPM:
npm install shrinkroute
API
[new] shrinkroute( [app][, routes][, separator = "."] )
Returns a new instance of Shrinkroute. This is a shortcut for the following:
var shrinkr = ;shrinkr;shrinkr;shrinkr;
.app( [app] )
Get or set the app of this Shrinkroute instance. If setting the app, the following things will be available from now on:
app.shrinkroute
- the Shrinkroute instancereq.route.name
- the name of the matched route
.route( name[, route] )
Get or set a route by its name. When setting the route, the route path must be passed as route.path
.
.route( name, path, route )
Set a route.
.route( [routes] )
Get all routes or set various routes at once.
.separator( [separator] )
Get or set the routes namespace separator. Useful for when using nested routes.
.url( name[, params][, append] )
Build the URL for a route. If the route path has parameters in the Express style (:param
), they must be passed as a object in the params
argument:
shrinkr;
If they're not passed, the returned URL will be blank. However, if you mark it as optional (:param?
), it'll not be required.
Parameters passed in the params
object that are not defined in the route will be appended to the query string, unless the append
argument is falsy.
shrinkr; // => /users?name=foo shrinkr; // => /users
.fullUrl( name[, params][, append] )
Builds full URLs for a given route that include protocol, host and port. Respects the same rules as .url()
.
req; // => http://foobar.com/users?name=foo
Testing
Shrinkroute is tested with Mocha. Inside the project root, run:
npm installnpm test
This will do the job for you!
License
MIT