Open vishwaefor opened 5 years ago
Step 1 GET route for retrieving all the articles GET /articles
router.get('/', (req, res, next) => { Articles.find() .populate('author') .then(results => { res.status(200).json({ results: results.map(r => { return { id: r.id, title: r.title, content: r.content, image: r.image, author: (r.author && { id: r.author._id, name: r.author.name }) || null }; }) }); }) .catch(err => next(err)); });
Step 2 GET route for retrieving all the articles GET /articles/:id
router.get('/:id', (req, res, next) => { Articles.findById(req.params.id) .populate('author', 'comments.author') .then( r => { if (r) { res.status(200).json({ id: r.id, title: r.title, content: r.content, image: r.image, author: (r.author && { id: r.author._id, name: r.author.name }) || null }); } else { const error = new Error('no article found'); error.status = 404; throw error; } }, err => { const error = new Error('invalid article id'); error.status = 400; throw error; } ) .catch(err => next(err)); });
Steps
Step 1 GET route for retrieving all the articles GET /articles
Step 2 GET route for retrieving all the articles GET /articles/:id