Processing a request containing a file upload

On the server, to process the request to the update API that may now contain a file, we will use the formidable npm module:

npm install --save formidable

Formidable will allow us to read the multipart form data, giving access to the fields and the file, if any. If there is a file, formidable will store it temporarily in the filesystem. We will read it from the filesystem, using the fs module to retrieve the file type and data, and store it to the photo field in the user model. The formidable code will go in the update controller in user.controller.js as follows.

mern-social/server/controllers/user.controller.js:

import formidable from 'formidable'import fs from 'fs'const update = (req, res, next) ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.