Node.js doesn’t handle file uploads by itself. To create an image uploader or a file uploader using Node.js, you’ll have to use a library or web framework that does the multipart form data handling for you. In this resource guide, you’ll find resources that cover these topics:-
- How to pick the best package or middleware
- Tutorials, tips, and code examples
- Other helpful resources
Building a Node.js File Uploader
Let’s get into it.
Node.js File Upload without Framework
Step one of building a Node.js uploader is to understand your setup. If you’re using a web framework, the support for file upload might be built-in. If you’re not using a framework, you’ll probably want to use one of the popular multipart form data handling packages, e.g. Multer, Busboy, Formidable, and Multiparty.
Not sure which to choose? Check out this helpful guide:-
byte archer: Choose between Formidable, Busboy, Multer and Multiparty for processing file uploads. Read it here.
Upload File: Node.js x Framework
As you’re know, Node.js is a runtime environment for JavaScript to run on the server side. When building an app or REST API, it’s commonly used together with a web framework such as ExpressJS, Socket.io, hapi, FeathersJS, MeteorJS, Total.js, etc.
Not all of these frameworks have the same support for file upload. In some cases, you might have to deploy the framework in conjunction with one of the multipart packages listed in the previous section.
Node.js x Express.js
Express.js doesn’t support file upload natively. You’ll need a middleware such as Multer.
- Official Express.js documentation: Multer. Express.js and Multer implementation. Code example for both front end and back end. See it here.
- envatotuts+: File Upload With Multer in Node.js and Express. Quite similar to the official example, but this tutorial has more explanation and it also shows how MongoDB is used for storage. See it here.
- Section.io: Uploading Files using Formidable in a Node.js Application. This tutorial uses Formidable and MongoDB. See it here.
Node.js x Feathers.js
Similar to Express.js, Feathers.js also requires a middleware in order to support file uploading.
- Feathers.js cookbook: File uploads in FeathersJS. See it here.
- This particular section of the recipe contains sample code for using Multer with Feathers.
Node.js x Socket.io
Socket.io uses web socket instead of HTTP. So its file upload solution is a little different from the others in this guide. Instead of multipart form data, Socket.io handles file transfer through socket programming. Here are some resources that will help you with your task.
Node.js Socket.io File Upload package. Documentation, installation steps, and code examples (client + server). See it here.
Node.js x hapi
hapi supports file upload natively. It makes creating a file uploader in Node a breeze. All you need is a few lines of code. hapi supports multipart and upload through stream.
Below are two tutorials that are very similar. But hopefully they will provide slightly different vantage points to you.
- akhromieiev: How to upload file in Hapi. See it here.
- Future Studio: hapi — How to Upload Files. See it here.
I wasn’t able to find an official example for file uploading, but the official documentation related to uploading files is found under the “payload” section.
Node.js x Meteor.js
Meteor.js is another framework that requires a middleware. And here are some ideas on how to use Multer and Meteor.js to implement file upload. See it here.
Node.js x Total.js
Total.js boasts file upload as one of its core features. Consequentially, the framework supports file upload natively.
Here’s some code example on how to upload files to Total.js’s FileStorage. See it here.
Other Resources Related to Node.js File Upload
So far we’ve covered ways to upload files TO a Node.js server, but what if you’re also interested in uploading files FROM a Node.js server? Here are some articles that shows how to upload files to Amazon Simple Storage Service (AWS S3) using Node.js.
- AWS Documentation: Creating and Using Amazon S3 Buckets – Official examples and explanation on using Node.js with AWS S3. See it here.
- Stack Abuse: Uploading Files to AWS S3 with Node.js – This tutorial takes a more detailed, hand-holding approach compared to the official doc. See it here.
Finally, I’m listing these IETF (Internet Engineering Task Force) specifications that are related to multipart form data. I came across them when researching for this article. I won’t say it’s useful for most developers, but if you’re interested in pulling your own uploader solution from scratch, these specs might come in handy.
- IETF RFC7578: Specs for multipart/form-data media type. See it here.
- IETF RFC2046: Specs for Multipurpose Internet Mail Extensions
(MIME). See it here.