How To Upload File In Node Js

Hello guys,

in this article, we will explain how to upload file in node this blog post simply using express.js file upload using multer is part of file upload in node js you will install multer library and get this package run this command.

Install & Visit This Site: Install Multer Module

Hello in this first you will create new app in node js

Step 1 : Create Node App For how to upload file in node js

create node app using below command

mkdir node_js_file_upload

cd node_js_file_upload

npm init

Read Also :  How To Push Array Element In Node Js

Step 2 : Install Embeded JavaScript templates (ejs)

npm install ejs

Step 3 : Install express.js in App

in this step 3 run below commande and install express.js

$ npm install express --save

Step 4 : Install multer in App

npm install multer

Step 5 : Create File_upload_form.ejs

create this html file in your project

<!DOCTYPE html>
	<title>How To Upload File Using Node.js - Laratuto</title>
    <h1>How To Upload File Using Node.js - Laratuto</h1>   
    <form action="/upload" enctype="multipart/form-data" method="POST">      
        <span>Upload File: </span>  
        <input type="file" name="file" required/> <br>
        <input type="submit" value="submit"> 

Step 6 : Create index.js file

const express = require("express")
const path = require("path")
const multer = require("multer")
const app = express()
app.set("view engine","ejs")
var storage = multer.diskStorage({
    destination: function (req, file, cb) {          
        cb(null, "uploads")
    filename: function (req, file, cb) {
      cb(null, file.fieldname + "-" +".jpg")
const maxSize = 1 * 1000 * 10000;
var upload = multer({ 
    storage: storage,
    limits: { fileSize: maxSize },
    fileFilter: function (req, file, cb){
        var filetypes = /jpeg|jpg|png/;
        var mimetype = filetypes.test(file.mimetype);
        var extname = filetypes.test(path.extname(
        if (mimetype && extname) {
            return cb(null, true);
        cb("Error: File upload only supports the "
                + "following filetypes - " + filetypes);
})"/upload",function (req, res, next) {
    upload(req,res,function(err) {
        if(err) {            
        else {            
            res.send("Successfully Image uploading Done..!")

Note : create uploads folder in app for store files.

Step 7 : Run index.js file

run this command below

node index.js
How To Upload File In Node Js

1 thought on “How To Upload File In Node Js”

Leave a Comment