April 2020
Intermediate to advanced
716 pages
18h 55m
English
We will set up a route to the photo stored in the database for each user, and also add another route that will fetch a default photo if the given user did not upload a profile photo. These routes will be defined as follows.
mern-social/server/routes/user.routes.js:
router.route('/api/users/photo/:userId') .get(userCtrl.photo, userCtrl.defaultPhoto)router.route('/api/users/defaultphoto') .get(userCtrl.defaultPhoto)
We will look for the photo in the photo controller method and, if found, send it in the response to the request at the photo route; otherwise, we'll call next() to return the default photo, as shown in the following code.
mern-social/server/controllers/user.controller.js:
const photo = (req, res, next) => { if(req.profile.photo.data){ ...Read now
Unlock full access