Configuration
first you have to install some libraray to your to your project
npm i express multer mongoose
index.js
app.use(express.static(__dirname('/public'))
//set folder as static folder
app.use(express.urlencoded({extended:false}))
//set req.body of data are accessible from index.html
`<form action="/api/post" method="post" enctype="multipart/form-data" >
<input type="text" name="name"/>
<input type="email" name="email"/>
<input type="file" name="file" id="file" />
<nput type="submit" />
</form>`
index.js
`const upload = multer({ dest: 'uploads/' })
const app = express()
//this code for single file upload
app.post('/api/post', upload.single('file'), function (req, res, next) {
const {name,email}=req.body
// req.file is the `file` file
res.send({name,email,filename:req.file.filename})
// req.body will hold the text fields, if there were any
})
`
Top comments (0)