Basic route is like this:
app.get('/', function(req, res){
res.send('hello world');
});
Is it possible to name that route and have it available in any template so it can be used like this:
app.get('/', name="index", function(req, res){
res.send('hello world');
});
Go to site <a href="{% url index %}">index page</a>.
Inspiration comes from Django 🙂
There is no out of the box mechanism for that. However you can mimic Django’s style like that: define
urls.jsfile which will hold an array of URLs. First start with:myviews.js
urls.js
Now in app.js ( or whatever the main file is ) you need to bind urls to Express. For example like this:
app.js
Now you can define custom helper ( Express 3.0 style ):
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 modifyurlfunction 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. 😀