April 2020
Intermediate to advanced
716 pages
18h 55m
English
To retrieve the uploaded photo, we will also set up a photo route endpoint that, on request, will return the photo associated with a specific post. The photo URL route will be defined with the other post-related routes, as follows.
mern-social/server/routes/post.routes.js:
router.route('/api/posts/photo/:postId').get(postCtrl.photo)
The photo controller will return the photo data stored in MongoDB as an image file. This is defined as follows.
mern-social/server/controllers/post.controller.js:
const photo = (req, res, next) => { res.set("Content-Type", req.post.photo.contentType) return res.send(req.post.photo.data)}
Since the photo route uses the :postID parameter, we will set up a postByID controller method to fetch ...