Read media API

To fetch media information for a specific media record, we will set up a route that accepts a GET request at '/api/media/:mediaId'.

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

router.route('/api/media/:mediaId')    .get( mediaCtrl.incrementViews, mediaCtrl.read)

The mediaId in the request URL will cause the mediaByID controller method to execute and attach the retrieved media document to the request object. Then this media data will be returned in the response by the read controller method.

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

const read = (req, res) => {  return res.json(req.media)}

A GET request to this API will also execute the incrementViews controller method, which will find the matching media record ...

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.