May 2018
Intermediate to advanced
470 pages
13h 54m
English
On the server, we will define an API to create the post in the database, starting with declaring a route to accept a POST request at /api/posts/new/:userId in mern-social/server/routes/post.routes.js:
router.route('/api/posts/new/:userId') .post(authCtrl.requireSignin, postCtrl.create)
The create method in the post.controller.js will use the formidable module to access the fields and the image file, if any, as we did for the user profile photo update.
mern-social/server/controllers/post.controller.js:
const create = (req, res, next) => { let form = new formidable.IncomingForm() form.keepExtensions = true form.parse(req, (err, fields, files) => { if (err) { return res.status(400).json({ error: "Image could not be uploaded" ...Read now
Unlock full access