File Upload in Node.js

This is the article all about the file upload in Node.js. A large number of applications allow users to upload profile pictures and other files. Therefore, files upload is a common requirement while building a REST API with Node.js & Express.

file upload in node js, Upload Files or Images to Server Using Node.js, NodeJS File Upload Using Multer, File Upload With Multer in Node.js and Express, node js rest api file upload, express file upload stream, node.js express upload image example.

Today we will show you how to upload file using REST API in Node.js. To manage the file upload on server, we are going to use express framework and middleware called multer. This middleware is designed for handling the multipart/form-data only.

Way to implement file upload in Node.js

  1. Setup REST API in Node.js using Express
  2. Install a dependency
  3. Handle multer storage
  4. Handle single file upload
  5. Handle multiple file upload
  6. Output

1. Setup REST API in Node.js using Express

We will be using the Node.js using Express framework to upload file. So We’ll need to have REST API in Node.js. Kindly follow the link below if you don’t know how to create REST API in Node.js using Express framework.

Create REST API in Node.js

2. Install a dependency

Run the following command to install the required dependencies.

On successful installation, You have to use it in server file to enable the middleware and handle the request. Checkout the below code of “server.js” where we use it.

3. Handle multer storage

As you know, we will be using the multer package to manage the uploads and already installed it in project, So the next step should be the create folder as “uploads” in the root directory where all our files will be saved.

Multer gives the option of storing files to server and also set the new identifier to the file, as shown below.

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express.

To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

4. Handle single file upload

Now we need to create a POST request in “server.js” file to handle the single uploaded file request. Following code will be used to manage it.

5. Handle multiple file upload

In order to handle multiple files with Multer is similar to a single file upload, but rather than .single() we are using .array(selector,fileLimit) of Multer.

In below code we mentioned upload.array('dataFiles', 10) that means multer will accept array of files limiting to max 10 file at each time. You can increase/decrease the number as you may need. Rest of the code is the same as single file upload.

6. Output

Finally, at last your “server.js” should look like below. You can have a test it with postman or any other tools.

Demo & Source Code

Github Repository Postman Collection
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *