Answer the question
In order to leave comments, you need to log in
Node.js, Express: url handling, how to implement?
Hello, tell me please. Django handles urls nicely
urlpatterns = [
url(r'^articles/2003/$', 'news.views.special_case_2003'),
url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
]
Answer the question
In order to leave comments, you need to log in
Have a look at this Express help section. It describes just that - how to set a controller for the url.
A few examples:
1) default index page handler:
app.get('/articles/2003/', function(req, res){
res.send('articles in 2003 year');
});
app.get(/^\/articles\/([0-9]{4})\/$/, function(req, res){
res.send('articles in ' + req.params[0] + ' year');
});
app.get('/user/:id', function(){
// ...
})
var controllers = require('./controllers');
app.get('/articles/2003/', controllers.special_case_2003);
app.get(/^\/articles\/([0-9]{4})\/$/, controllers.year_archive);
app.get('/user/:id', controllers.user);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question