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

`

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more