Ishbir
Ishbir

Reputation: 390

URL Generation for Routes in Express

I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.

I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.

My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.

Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?

UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.

Upvotes: 18

Views: 8558

Answers (3)

Hendy Irawan
Hendy Irawan

Reputation: 21424

From @freakish's answer:

There is no out of the box mechanism for that. However you can mimic Django's style like that: define urls.js file which will hold an array of URLs. First start with:

myviews.js

exports.Index = function( req, res, next ) {
    res.send( "hello world!" );
};

urls.js

var MyViews = require( "mywviews.js" );

module.exports = [
    { name : "index", pattern : "/", view : MyViews.Index }
]

Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:

app.js

var urls = require( "urls.js" );

for ( var i = 0, l = urls.length; i < l; i++ ) {
    var url = urls[ i ];
    app.all( url.pattern, url.view );
};

Now you can define custom helper ( Express 3.0 style ):

var urls = require( "urls.js" ), l = urls.length;
app.locals.url = function( name ) {
    for ( var i = 0; i < l; i++ ) {
        var url = urls[ i ];
        if ( url.name === name ) {
            return url.pattern;
        }
    };
};

and you can easily use it in your template. Now the problem is that it does not give you fancy URL creation mechanism like in Django ( where you can pass additional parameters to url ). On the other hand you can modify url function and extend it. I don't want to go into all details here, but here's an example how to use regular expressions ( you should be able to combine these to ideas together ):

Express JS reverse URL route (Django style)

Note that I posted the question, so I had the same problem some time ago. :D

Upvotes: 0

antitoxic
antitoxic

Reputation: 3844

Or stick with express and use the package reversable-router.

Example from the readme:

app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){
    //...
});

//.. and a helper in the view files:
url('admin.user.edit', {id: 2})

Upvotes: 15

Luke Vivier
Luke Vivier

Reputation: 326

You might try Locomotive, which is built on Express.

It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."

Locomotive's router generates helpers that are automatically available to controllers and views.

Upvotes: 10

Related Questions