{"id":17498,"date":"2021-04-02T08:01:28","date_gmt":"2021-04-02T08:01:28","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=17498"},"modified":"2026-02-05T05:57:00","modified_gmt":"2026-02-05T05:57:00","slug":"node-js-multer","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/node-js-multer","title":{"rendered":"Mastering File Uploads in Node.js and Express.js [2026 Tutorial]"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>File upload is the most basic operation for any application. Regarding Node, file upload with Multer in Node.js and Express is efficient and seamless. It is designed to simplify handling incoming files and efficiently store, receive, and upload it.<\/p>\n<p>Today we will discuss and learn about file uploading in Nodejs Multer. Also, we will see how to upload images\/videos using Multer in node js and express. Let\u2019s begin our tutorial and learn about <b>node js Multer<\/b> file upload.<\/p>\n<h2>What is Multer in Nodejs?<\/h2>\n<p><i>Node js Multer<\/i> is a middleware in node.js that handles multipart\/form-data, which is principally used for uploading files. It is written on top of the busboy for maximum efficiency.<\/p>\n<p>As a result, Multer makes the process of uploading files in Nodejs easier. It is an NPM package that tackles the complexities of file handling by integrating the middleware stack of Node applications. <\/p>\n<p>Additionally, it allows renaming files, setting sizes, different types, and filtering files. <\/p>\n<h2>Steps-by-step tutorial of File Upload Using Multer in Node.js and Express<\/h2>\n<p>how to use multer in node js?<br \/>\nFollowing is the step-by-step guide to learning about how to upload Node Multer files.  <\/p>\n<h3>Initial Setup<\/h3>\n<p><b><u>Create a directory<\/u><\/b><\/p>\n<pre>mkdir MulterApp\r\ncd MulterApp<\/pre>\n<p><b><u>Define package.json file<\/u><\/b><\/p>\n<p>For creating one, run this command:<\/p>\n<pre>npm init<\/pre>\n<h3>Install dependencies<\/h3>\n<p>We only have two dependencies to be installed- Express and Multer. So here are the commands<br \/>\n<b><u>To install Express-<\/u><\/b><\/p>\n<pre>npm install express --save<\/pre>\n<p><b><u>To install Multer-<\/u><\/b><br \/>\nhow to install multer in node js?<\/p>\n<pre>npm install --save multer<\/pre>\n<p>Now, it\u2019s time to code. <\/p>\n<p>First of all, <u>create a file<\/u> and name it anything; here it is <strong><em>app.js.<\/em><\/strong> I\u2019ll be coding the entire code in <em>app.js<\/em> to keep it simple for you. Further, <u>loading the express module<\/u> with the help of  <strong>require()<\/strong> and then, at last, writing the below-mentioned code for <u>setting up the basic express server<\/u><\/p>\n<p><b>\/\/app.js<\/b><\/p>\n<pre>const express = require('express');\r\nconst path = require('path');\r\nconst app = express()\r\nconst port = process.env.PORT || 3000\r\napp.get(\u2018\/\u2019, (req, res) => { \r\n    res.send(\u2018Hello People\u2019); \r\n});\r\napp.listen(port, () => {\r\n    console.log('Server is up on port ' + port);\r\n})\r\n<\/pre>\n<p>Run this command to verify-<\/p>\n<pre>node app.js<\/pre>\n<p>Hit <strong>http:\/\/localhost:3000<\/strong>, and you should see \u201c<b>Hello People<\/b>\u201d in your window. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/04\/hello-people-min-1.png\" alt=\"hello-people\" width=\"700\" height=\"266\" class=\"alignnone size-full wp-image-30022\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/04\/hello-people-min-1.png 700w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/04\/hello-people-min-1-300x114.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<h3>Adding Multer<\/h3>\n<p>For simplicity purpose, I\u2019ll store all the uploaded files in the local folder. Write the below-mentioned code for loading multer in our app.js file<\/p>\n<pre>const multer = require(\u2018multer\u2019);<\/pre>\n<h3>Multer Storage for Single Image Upload<\/h3>\n<p>Now, the next thing to be implemented is to define a storage location for all the files. Multer made it easy by providing an option to store our files to disk. We will set up a directory to save all the files and provide them a new identifier.<\/p>\n<p>Create a folder- <b><em>images<\/em><\/b>, in the root directory.<\/p>\n<p><b>\/\/ Image Upload<\/b><\/p>\n<pre>const imageStorage = multer.diskStorage({\r\n    \/\/ Destination to store image     \r\n    destination: 'images', \r\n      filename: (req, file, cb) => {\r\n          cb(null, file.fieldname + '_' + Date.now() \r\n             + path.extname(file.originalname))\r\n            \/\/ file.fieldname is name of the field (image)\r\n            \/\/ path.extname get the uploaded file extension\r\n    }\r\n});\r\n<\/pre>\n<p><b><em>Destination <\/em><\/b>&#8211; Destination is used so that the application can know where to store images. It can be a string (e.g. \u2018.\/upload\u2019). The default directory for all the temporary files is used if the destination is unavailable. Creating a directory is compulsory when you use the destination as a function. Or else, if you use destination as string, Multer will create directory.<\/p>\n<p><b><em>Filename<\/em><\/b> &#8211; Filename determines what a file should be named in the folder.<br \/>\nA random file name with no extension will be given if you don\u2019t provide the file\u2019s name. The function takes three parameters &#8211; the request object, the file object, and a callback function. The two arguments to the <\/p>\n<ul class=\"bullets text-left\">\n<li>null: as we aren\u2019t showing an error.<\/li>\n<li>file.originalname \u2013 in this demo, I have used the same file name as they are uploaded. You can choose any name.<\/li>\n<\/ul>\n<p>We have used the Multer() method method in the below-mentioned snippet- <\/p>\n<pre>const imageUpload = multer({\r\n      storage: imageStorage,\r\n      limits: {\r\n        fileSize: 1000000 \/\/ 1000000 Bytes = 1 MB\r\n      },\r\n      fileFilter(req, file, cb) {\r\n        if (!file.originalname.match(\/\\.(png|jpg)$\/)) { \r\n           \/\/ upload only png and jpg format\r\n           return cb(new Error('Please upload a Image'))\r\n         }\r\n       cb(undefined, true)\r\n    }\r\n}) \r\n<\/pre>\n<p>The Multer() method method takes an object with storage property,<br \/>\nThe <b><em>limits<\/em><\/b> property describes the maximum size of the file.<\/p>\n<p>The <b><em>fileFilter<\/em>()<\/b> method is for security reasons. I have validated files before it is uploaded to the servers. Here, it will accept only two extensions &#8211; .png and .jpg.<\/p>\n<p>After this, it\u2019s time to create the post route. We will make a POST request to <b><em>localhost:3000\/uploadImage.<\/em><\/b><br \/>\nAdd the following code in the <em>app.js-<\/em><\/p>\n<pre>\/\/ For Single image upload\r\nrouter.post('\/uploadImage', imageUpload.single('image'), (req, res) => {\r\n     res.send(req.file)\r\n}, (error, req, res, next) => {\r\n     res.status(400).send({ error: error.message })\r\n})\r\n<\/pre>\n<p>I have used <b>imageUpload.single(\u2018image\u2019)<\/b> for uploading a single file. As mentioned earlier, the multer adds a file object to the request. The file object has metadata of the file.<\/p>\n<p>Here inside the single() method, we have passed the field name. The same field name we will pass in Postman.<br \/>\nTo test the endpoint, open Postman.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/scknB5ywVeQ\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>Keep <strong><em>key name<\/em><\/strong> or the <strong><em>field name<\/em><\/strong> same as those given in the <strong><em>imageUpload.single()<\/em><\/strong>. Check the image in the <strong><em>Images <\/em><\/strong>folder. <\/p>\n<h3>Upload Multiple Images using Multer in Node JS<\/h3>\n<p>Now we will upload multiple files using Multer. For that, Multer provides another function <b>arrays(fieldname[, max_count])<\/b> that takes an array of files, all with the name <em>fieldname<\/em>. It will generate an error in case you upload more than <em>max_count<\/em> files. The array of files will be saved in <em>req.files.<\/em><\/p>\n<p>Here no need to create any storage. We will use the same storage as before.<\/p>\n<pre>\/\/ For multiple image upload\r\n\r\nrouter.post('\/uploadBulkImage', imageUpload.array('images', 4),     (req, res) => {\r\n   res.send(req.files)\r\n}, (error, req, res, next) => {\r\n    res.status(400).send({ error: error.message })\r\n})\r\n<\/pre>\n<p>Open Postman, enter the URL, select multiple files and click Send.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/srlmGNeFqDI\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>Verify the image in the <strong><em>Images <\/em><\/strong>folder.<\/p>\n<h3>Upload Video Files using Multer<\/h3>\n<p>Let\u2019s further learn about video file upload in node js using Multer. As discussed above, we have to create diskStorage to upload the video. And for that, use the below-mentioned snippet-<\/p>\n<pre>\/\/ Video Upload<\/pre>\n<pre>\r\nconst videoStorage = multer.diskStorage({\r\n     destination: 'videos', \/\/ Destination to store video \r\n     filename: (req, file, cb) => {\r\n         cb(null, file.fieldname + '_' + Date.now() \r\n          + path.extname(file.originalname))\r\n     }\r\n});\r\n<\/pre>\n<p>Use multer() method to upload video the way we did for images.<\/p>\n<pre>const videoUpload = multer({\r\n     storage: videoStorage,\r\n     limits: {\r\n     fileSize: 10000000 \/\/ 10000000 Bytes = 10 MB\r\n     },\r\n     fileFilter(req, file, cb) {\r\n       \/\/ upload only mp4 and mkv format\r\n       if (!file.originalname.match(\/\\.(mp4|MPEG-4|mkv)$\/)) { \r\n          return cb(new Error('Please upload a video'))\r\n       }\r\n       cb(undefined, true)\r\n    }\r\n})\r\n<\/pre>\n<p>After this, create a <strong><em>POST <\/em><\/strong>request to upload the video.<\/p>\n<pre>router.post('\/uploadVideo', videoUpload.single('video'), (req, res) => {\r\n   res.send(req.file)\r\n}, (error, req, res, next) => {\r\n    res.status(400).send({ error: error.message })\r\n})\r\n<\/pre>\n<p>Test the API in Postman as explained above.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/853rkEab-E4\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>Check the video in the <strong><em>videos<\/em> <\/strong>folder. You can find the source code here &#8211; <strong><a href=\"https:\/\/github.com\/happy-bhesdadiya\/file-upload\" target=\"_blank\" rel=\"noopener\">Github Repository<\/a><\/strong><\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Our clients say Bacancy provides the best and highly-skilled developers!<\/span><br \/>\nWant the best? Get the best! Contact Bacancy and <a href=\"https:\/\/www.bacancytechnology.com\/hire-node-developer\" target=\"_blank\" rel=\"noopener\">hire Node.js developer<\/a> for your dream projects!<\/i><\/strong><\/p>\n<h2>Conclusion<\/h2>\n<p>I hope you found this tutorial on how to upload files(Video\/Image) using Node Multer helpful and have learned about image and video file upload using Multer in NodeJs and Express.js. File upload can be an exciting feature, but its implementation doesn\u2019t have to be difficult always. We can handle multipart\/form-data at our ease using Node js Multer example.<\/p>\n<p>If you are looking for file upload in node js using Multer and file upload in Express.js using Multer, then you have landed on the right page. At Bacancy Technology, we hold a pool of skilled Node.js developers whose expertise can be leveraged for handling multipart and form-data. Hire Nodejs developer from us to integrate file upload in Node js using Multer in any application. <\/p>\n<p>Build your next-gen application with your choice of frontend and integrate the file uploads based on the knowledge acquired from Nodejs tutorials. <\/p>\n<p>Keep Learning and Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction File upload is the most basic operation for any application. Regarding Node, file upload with Multer in Node.js and Express is efficient and seamless. It is designed to simplify handling incoming files and efficiently store, receive, and upload it. Today we will discuss and learn about file uploading in Nodejs Multer. Also, we will [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":17506,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[483],"tags":[],"coauthors":[1585,2124],"class_list":["post-17498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js"],"acf":[],"modified_by":"Aditya Goswami","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/17498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=17498"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/17498\/revisions"}],"predecessor-version":[{"id":57536,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/17498\/revisions\/57536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/17506"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=17498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=17498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=17498"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=17498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}