Related list API

In order to retrieve the list of related media from the database, we will set up an API on the server that will receive a GET request at '/api/media/related/:mediaId'.

mern-mediastream/server/routes/media.routes.js:

router.route('/api/media/related/:mediaId')        .get(mediaCtrl.listRelated)

The listRelated controller method will query the Media collection to find records with the same genre as the media provided, and also exclude this media record from the results returned. The results returned will be sorted by the highest number of views and limited to the top four media records. Each media object in the returned results will also contain the name and ID of the user who posted the media.

mern-mediastream/server/controllers/media.controller.js ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.