From @ weird answer :
There is no ready-made mechanism for this. However, you can mimic the Django style as follows: define a urls.js file that will contain an array of URLs. 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 any other main file) you need to bind the URL 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 a 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 a fancy mechanism for creating URLs, as in Django (where you can pass additional parameters to url ). Alternatively, you can change the url function and extend it. I don't want to go into details, but here is an example of using regular expressions (you should be able to combine them with ideas together):
JS Reverse URL Express Route (Django Style)
Please note that I posted a question, so I had the same problem a while ago. : D
source share