May 2018
Intermediate to advanced
470 pages
13h 54m
English
To retrieve the uploaded photo, we will also set up a photo route URL that returns the photo with a specific post.
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.
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)}
As the photo route uses the :postID parameter, we will set up a postByID controller method to fetch the specific post by its ID before returning to the photo request. We will add the param call to post.routes.js.
mern-social/server/routes/post.routes.js ...
Read now
Unlock full access