express-named-router

Don't use URL literals in your applications. Express Named Router is a wrapper for Express JS routes definition which allows named routing.

express-named-router

A wrapper for Express JS routes definition which defines named routes

npm install express-named-router

Just initialize the wrapper like this

var namedRouter = require('express-named-router');
 
...
 
namedRouter.initForApplication(app, {
    registerAppHelpers: true //Makes named routes accessibles in app.locals
});

You can still register your routes as usual, but if you want you can

app.namedRoute('contacts', {via: 'get', path: '/contact', handler: routes.contact.index, middlewares: []});

In your app.locals, you will find contactsPath() = '/contacts', which you can use in your views. For your node scripts, use namedRouter.get('contacts') for the same purpose.

When calling, let's say with the previous example, contactsPath(), and the associated route have parameters (i.e. contacts/:type would have the :type parameter in it), you can specify them like this

contactsPath({
    type'cellPhone'
});

This would return '/contacts/cellPhone'. If 'type' is not found in the parameters, it will be appended as query string parameter.

Parameters with non-matching arguments will be ignored.

NOT SO STABLE but working

This is usefull for "static" routes, but not so usefull when dealing with complex regexp routes. You can still use the named routes to register routes with regexp in the express router, but the returned string (the regexp) would be pointless for view rendering purposes.

Released under BSD license.