DEV Community

Cover image for How we upload File with some data to nodeJS using Simple Form
Deepak Jaiswal
Deepak Jaiswal

Posted on • Edited on

2 1 1 1 1

How we upload File with some data to nodeJS using Simple Form

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
Enter fullscreen mode Exit fullscreen mode
`<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>`
Enter fullscreen mode Exit fullscreen mode

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
})
Enter fullscreen mode Exit fullscreen mode

`

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay