April 2020
Intermediate to advanced
716 pages
18h 55m
English
On the server, we will define an API to create the post in the database, starting by 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 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. The create controller method will look as follows.
mern-social/server/controllers/post.controller.js:
const create = (req, res, next) => { let form = new formidable.IncomingForm() form.keepExtensions = true form.parse(req, async (err, fields, files) => { if (err) { return res.status(400).json({ ...Read now
Unlock full access