May 2018
Intermediate to advanced
470 pages
13h 54m
English
In order to retrieve specific lists of media from the database, we need to set up relevant APIs on the server. For popular media, we will set up a route that receives a GET request at /api/media/popular.
mern-mediastream/server/routes/media.routes.js:
router.route('/api/media/popular') .get(mediaCtrl.listPopular)
The listPopular controller method will query the Media collection to retrieve ten media documents that have the highest views in the whole collection.
mern-mediastream/server/controllers/media.controller.js:
const listPopular = (req, res) => { Media.find({}).limit(10) .populate('postedBy', '_id name') .sort('-views') .exec((err, posts) => { if (err) { return res.status(400).json({ error: errorHandler.getErrorMessage(err) ...Read now
Unlock full access