Retrieve and stream media
On the server, we will set up a route to retrieve a single video file, which we will then use as a source in a React media player to render the streaming video.
Get video API
We will add a route in the media routes to fetch a video when a GET request is received at '/api/medias/video/:mediaId'
.
mern-mediastream/server/routes/media.routes.js
:
router.route('/api/medias/video/:mediaId') .get(mediaCtrl.video) router.param('mediaId', mediaCtrl.mediaByID)
The :mediaId
parameter in the route URL will be processed in the mediaByID
controller to fetch the associated document from the Media collection and attached to the request object, so it may be used in the video
controller method as required.
mern-mediastream/server/controllers/media.controller.js
:
const mediaByID = (req, res, next, id) => { Media.findById(id).populate('postedBy', '_id name').exec((err, media) => { if (err || !media) return res.status('400').json({ error: "Media not found"...