This tutorial shows you how to upload, display and save images in node.js using the express-fileupload, formidable, and multer modules. Apart from the file upload modules, we will use the express server, ejs templating engine, and nodemon to restart the server.
It assumes you want to take a file input on a form, upload it to a Node.js server, and save it before displaying it as a static asset.
Let's get started.
Include the upload method post, encoding type multipart/form-data, and file's name in the form.
Download the (image1, image2, and image3) images. We will be uploading them to the public/images folder of the server.
Using the express.static() function implemented in each index.js file, we will let the frontend to fetch images from the server automatically.
Here is how to implement each module in the index.js file.
How to upload, display and save images in node.js examples
Example~1: How to upload, display and save images in node.js using the express-upload module
Initialize a package.json file, then install the third-party packages.
cd expressUpload
npm init -y
npm i ejs express express-ejs-layouts express-fileupload
npm i -g nodemon
Using the mv() function (provided by the module), we upload the image to the specified destination.
file.mv(filePath, err => {
if (err) return res.status(500).send(err)
res.redirect('/')
})
Lastly, we run the callback function checking if there was an error uploading the image to the server. Otherwise, we redirect the user to the home page.
Start the server.
npm start
Open port 3000 on the browser and try uploading an image.
Before uploading an imageAfter uploading the 3 images
Example~2: How to upload, display and save images in node.js using the formidable module
Initialize a package.json then install the third-party packages.
cd ..
cd formidable
npm init -y
npm i ejs express express-ejs-layouts formidable
We create an instance of the form data (from the client-side) and store the result in the form variable.
const form = new formidable.IncomingForm()
Using the object's uploadDir attribute, we tell the module to store the uploaded file in the public directory's images directory referenced by the filePath variable. Using other attributes like multiples and maxFileSize, we could control file size and whether we need multiple uploads.
Next, we parse the files in the object's parse() method, which takes the request object and a callback function returning error, fields, and files objects.
The diskStorage() function allows us to store files on the disk. We pass it to two objects, each accepting the request object, file, and a callback function. The callback checks for any error (null) before handling file saving as follows:
destination: We tell the module to save images in the images directory inside the public directory.filename: We tell multer to save the image's original name in the specified destination.
We then ship the packed destination and filename to multer and run the middleware before hitting the /uploads endpoint.
Start the server.
npm start
Open port 5000 on the browser and upload the images.
Conclusion
Knowing how to upload, display and save images in node.js is a key step toward handling images in a Node.js server. After uploading the images, you can save them on the server, upload them to a CDN or save them in a database.
This tutorial walked you through one of the most challenging parts: uploading images to a Node.js server. You saw how to upload images with the express-fileupload, formidable and multer modules. Next, you saved the images in a public directory and displayed them on the landing page.
You can find the complete code on GitHub.
Views:563
Steve Alila specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.
Can't find what you're searching for? Let us assist you.
Enter your query below, and we'll provide instant results tailored to your needs.
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.