Skip to Content
Full-Stack React Projects - Second Edition
book

Full-Stack React Projects - Second Edition

by Shama Hoque
April 2020
Intermediate to advanced
716 pages
18h 55m
English
Packt Publishing
Content preview from Full-Stack React Projects - Second Edition

Retrieving a post's photo

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Full-Stack React Projects

Full-Stack React Projects

Shama Hoque
React Native - The Practical Guide

React Native - The Practical Guide

Academind by Maximilian Schwarzmüller GmbH

Publisher Resources

ISBN: 9781839215414Supplemental Content