,

Learn how to receive multipart/form-data in Node JS and upload files to the web with Austin Gil’s web development tutorial.

Posted by

Uploading Files to the Web

Uploading Files to the Web: Receiving multipart/form-data in Node JS

Whether you’re building a social media platform, a photo sharing app, or an e-commerce website, allowing users to upload files to the web is essential. In this article, we’ll explore how to receive multipart/form-data in Node JS, a popular backend language for web development.

What is multipart/form-data?

When a user uploads a file to a web server, the data is sent as a “multipart/form-data” request. This type of request is commonly used for file uploads and allows the client to send multiple sets of data in a single request. In Node JS, we can use the “multer” middleware to handle multipart/form-data requests and process the uploaded files.

Using multer to receive multipart/form-data

First, we need to install the “multer” package using npm:

npm install multer

Once installed, we can require the “multer” package in our Node JS application and set up a storage configuration to define where the uploaded files will be stored on the server:


var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/') // specify the folder where uploaded files will be stored
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })

Next, we can use the “upload” middleware to process the incoming multipart/form-data requests and save the uploaded files to the specified destination:


app.post('/upload', upload.single('file'), function (req, res, next) {
// file has been uploaded and saved to the server
res.send('File uploaded successfully');
})

Conclusion

Receiving multipart/form-data in Node JS is a crucial step in building web applications that allow users to upload files. With the “multer” middleware, we can easily process and save the uploaded files to the server, giving us the flexibility to build a wide range of file upload features for our web applications.

For more articles and tutorials on web development, be sure to check out the Learn Web Dev with Austin Gil website.

0 0 votes
Article Rating
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@frankjuuh
7 months ago

What if I am using something like Cloudflare Pages for hosting the Nuxt app? I don't think I have access to the file system. And I'm supposed to eventually save the file to an R2 bucket.

@crqay
7 months ago

Hey @akamai. A small suggestion: The background music in this video is distracting and clashes with what you are teaching. apart from that, this was a helpful video! Cheers.

@riversound8871
7 months ago

I am able to send data through raw by postman, but through form-data it is throwing error. Can you please guide something so that I can improve my Node.js api?

@md.sultanularefin2979
7 months ago

very good video, thank you.

@jeanpierrejeri6425
7 months ago

Hi! and what about multer? is there any differences with formidable? thanks for the video!