Node JS Express Rest API File Upload Example

Node JS Express Rest API File Upload Example

Node js express file upload rest API example; This tutorial will show you how to upload files in node js + express + rest APIs using multer package.

Throughout this tutorial steps, you will learn how to create file upload REST API with Node js and Express js. You will also find out how to use multer in Node.js for handling multipart/form-data for uploading files.

Node JS + Express + Rest API + File Upload + Multer

Follow the below steps to file upload with multer in node.js + express + rest API;

  • Step 1 – Create Node Express js App
  • Step 2 – Install express and Multer dependencies
  • Step 3 – Create Server.js File
  • Step 4 – Start Node Express Js App Server
  • Step 5 – Upload File using Node js Rest Api

Step 1 – Create Node Express js App

Execute the following command on terminal to create node js app:

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

Step 2 – Install express and Multer dependencies

Execute the following command on terminal to install express and busboy dependencies:

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

Step 3 – Create Server.js File

Create server.js file and import express, multer, path dependencies in server.js; as shown below:

var express =   require("express");
var multer  =   require('multer');
var bodyParser = require('body-parser');

var app         =   express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now());
  }
});
var upload = multer({ storage : storage}).single('userPhoto');

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

app.listen(3000,function(){
    console.log("Working on port 3000");
});

Step 4 – Start Node Express Js App Server

Execute the following command on terminal to start node express js server:

//run the below command

npm start

after run this command open your browser and hit 

http://127.0.0.1:3000/upload-avatar

Step 5 – Upload File using Node js Rest Apis

To upload files using rest apis; So open postman for sending HTTP multipart/form-data requests: as shown below picture:

Conclusion

Node js express rest API file upload example tutorial; you have learned how to build REST API for file uploading using rest API in Node.js, Express.js and Multer.

Recommended Node JS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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