Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...
- // endpoint to list all articles
- router.get('/', auth.optional, function(req, res, next) {
- // set query, limit, offset params for filtering/pagination.
- var query = {};
- var limit = 20;
- var offset = 0;
- if(typeof req.query.limit !== 'undefined'){
- limit = req.query.limit;
- }
- if(typeof req.query.offset !== 'undefined'){
- offset = req.query.offset;
- }
- // return promise for [articles matching query, count, user]
- return Promise.all([
- Article.find(query)
- .limit(Number(limit))
- .skip(Number(offset))
- .sort({createdAt: 'desc'})
- .populate('author')
- .exec(),
- Article.count(query).exec(),
- req.payload ? User.findById(req.payload.id) : null,
- ]).then(function(results){
- var articles = results[0];
- var articlesCount = results[1];
- var user = results[2];
- // return list of articles and articlesCount
- return res.json({
- articles: articles.map(function(article){
- return article.toJSONFor(user);
- }),
- articlesCount: articlesCount
- });
- }).catch(next);
- });
- //...
Advertisement
Add Comment
Please, Sign In to add comment