Node js Express Rest Api File Upload using Multer Example

In this tutorial, you will learn how to create file upload REST API in Node js Express js application for handling multipart/form-data for uploading files.

How to Upload File using Multer with Node Express js Rest Api?

Here are steps:

Step 1 – Set Up Node.js Application

Simply start your cmd or terminal window and run the following commands into it to create node js application or project:

mkdir my-app
cd my-app
npm init -y
mkdir uploads

Step 2 – Install Express and Multer Module

Run the following command on cmd to install express and multer modules:

npm install express multer body-parser --save
npm install sharp --save

Step 3 – Create Server.js File and Import Modules

Navigate to node express js project root folder and create server.js file, and then import express, multer, and path dependencies into it; like following:

var express =   require("express");
var multer  =   require('multer');
var bodyParser = require('body-parser');
var app  =   express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
   extended: false
}));
app.use(bodyParser.json());
var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads/');
  },
  filename: function (req, file, callback) {
    callback(null, file.originalname);
  }
});
var upload = multer({ storage : storage}).single('file');
app.listen(3000,function(){
    console.log("Working on port 3000");
});

Step 4 – Create Route for Rest API to Upload File

To create a file upload route using the REST API, simply create it like the following and add it to the server.js file:

app.post('/file-upload', function (req, res) {
    upload(req,res,function(err) {
        if(err) {
            return res.end("Error uploading file.");
        }
        res.end("File is uploaded");
    });
});

Step 5 – Start Node Express Js App Server

Run the npm start on cmd or terminal window to start the node express js server:

//run the below command

npm start

Step 6 – File Upload Rest API in Node js using Postman

Simply start your Postman application to upload files using the rest APIs in the node express js project; as shown below picture:

file upload in node js using postman via rest api
file upload in node express js using postman via rest api

Conclusion

That’s it; You have learned how to create a REST API for uploading files in Node Express JS with Multer.

Recommended Node JS Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *