How To Upload File In Node Js

Hello guys,

in this article, we will explain how to upload file in node js.in 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>
<html>
<head>
	<title>How To Upload File Using Node.js - Laratuto</title>
</head>
<body>
    <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"> 
    </form>
</body>
</html>

Step 6 : Create index.js file

const express = require("express")
const path = require("path")
const multer = require("multer")
const app = express()
    
app.set("views",path.join(__dirname,"views"))
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 + "-" + Date.now()+".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(
                    file.originalname).toLowerCase());
        
        if (mimetype && extname) {
            return cb(null, true);
        }
      
        cb("Error: File upload only supports the "
                + "following filetypes - " + filetypes);
      } 
 
}).single("file");       
  
app.get("/",function(req,res){
    res.render("File_upload_form");
})
    
app.post("/upload",function (req, res, next) {
         
    upload(req,res,function(err) {
  
        if(err) {            
            res.send(err)
        }
        else {            
            res.send("Successfully Image uploading Done..!")
        }
    })
})    
app.listen(8000);

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