Node JS Tutorial in Hindi #58 Upload File using Multer NPM Package
Upload file in Node js
- Make Get and post route
- Make Html form to submit file
- Install package
- Write code to upload file
- Test and verify issues
- Interview Questions
- 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)