Edited for your own purpose, have 3 different options in one answer. Thank @ hjpotter92 for his regular request.
URL Parameter
With regex
app.get('/articles/:year?/:month?/:day?', function(req, res) { var year = req.query.year;
No regex
var getArticles = function(year, month, day) { ... } app.get('/articles/:year', function(req, res) { getArticles(req.params.year); } app.get('/articles/:year/:month', function(req, res) { getArticles(req.params.year, req.params.month); } app.get('/articles/:year/:month/:day', function(req, res) { getArticles(req.params.year, req.params.month, req.params.day); }
Define 3 paths you want to support and reuse the same function
With request parameters
app.get('/articles', function(req, res) { var year = req.query.year;
The URL for this endpoint will look like this:
http:
R. gulbrandsen
source share