Node JS Tutorial in Hindi #58 Upload File using Multer NPM Package

Upload file in Node js


  1. Make Get and post route
  2. Make Html form to submit file
  3. Install package
  4. Write code to upload file
  5. Test and verify issues
  6. Interview Questions
  7. Notes, Code and playlist.


index.js file code


create upload folder in project

import express from 'express'
import multer from 'multer';

const app = express();
const storage = multer.diskStorage({
destination:function (req,file,cb) {
cb(null,'upload')
},
filename:function (req,file,cb) {
cb(null,file.originalname)
},
})
const upload= multer({storage})

app.get("/",(req,resp)=>{
resp.send(`
<form action='/upload' method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
<button>Upload file</button>
</form>
`)
})

app.post("/upload", upload.single('myfile'), (req,resp)=>{
resp.send({
message:'file uploaded',
info:req.file
})
})

app.listen(3200)