I try to make an API for my next website and I have trouble to get multiple query results with the same url in an express app.
Dummy data:
var data = [{
articles : [{
id : '0',
url : 'foo',
title : 'Foo',
body : 'some foo bar',
category : 'foo',
tags : [
'foo'
]
}, {
id : '1',
url : 'foo-bar',
title : 'Foo bar',
body : 'more foo bar',
category : 'foo',
tags : [
'foo', 'bar'
]
}, {
id : '2',
url : 'foo-bar-baz',
title : 'Foo bar baz',
body : 'more foo bar baz',
category : 'foo',
tags : [
'foo',
'bar',
'baz'
]
}]
}, {
users : [{
name: 'Admin'
}, {
name: 'User'
}]
}];
Router:
// Grabs articles by categories and tags
// http://127.0.0.1:3000/api/articles/category/foo/tag/bar
router.get('/articles/category/:cat/tag/:tag', function(req, res) {
var articles = data[0].articles;
var q = articles.filter(function (article) {
return article.category === req.params.cat;
return article.tags.some(function(tagId) { return tagId === req.params.tag;});
});
res.json(q);
});
How I can nest the results if I requesting the http://127.0.0.1:3000/api/articles/category/foo/tag/bar url? Now if I do this, tag url is ignored, only category requests have effect.
Thank You for your help!