How to upload files in Node.js and Express

Modern mobile apps and websites allow users to upload profile pictures and other files. Handling file upload is a common requirement while building a REST API with Node.js & Express.

In this tutorial, we will go discuss how to handle single and multiple file uploads with Node.js and Express backend and save uploaded files on the server.


First, let us create a new Node.js app by running the below commands. I am using npm for package management. It should be fine to use yarn if you like.

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 

-y or --yes skips the interaction session and generates a package.json file based on your defaults.

Next, run the following command to install the required dependencies:

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 

Here is what each of the above packages does:

  • express - Popular web framework built on top of Node.js. We shall use it for developing REST API.
  • body-parser - Node.js request body parsing middleware parses the incoming request body before your handlers and makes it available under the req.body property. In short, it simplifies incoming requests.
  • cors - Another Express middleware for enabling CORS (Cross-Origin Resource Sharing) requests.
  • express-fileupload - Simple Express middleware for uploading files. It parses multipart/form-data requests, extracts the files if available, and makes them available under the req.files property.
  • morgan - Node.js middleware for logging HTTP requests.
  • lodash - A JavaScript library that provides utility functions for arrays, numbers, objects, strings, etc.

Create Express Server

After installing the required dependencies, let us start creating our Express server.


const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const _ = require('lodash');

const app = express();

// enable files upload
    createParentPath: true

//add other middleware
app.use(bodyParser.urlencoded({extended: true}));

//start app 
const port = process.env.PORT || 3000;

app.listen(port, () => 
  console.log(`App is listening on port ${port}.`)

The above code is straightforward. First, it setups the express-fileupload middleware to enable multipart/form-data requests. Afterward, other Express middleware are added to allow Cross-Origin Resource Sharing (CORS), request body parsing, and HTTP request logging. In the end, it starts the server at port 3000.

Upload Single File

Let us create our first route to allow users to upload their profile pictures.'/upload-avatar', async (req, res) => {
    try {
        if(!req.files) {
                status: false,
                message: 'No file uploaded'
        } else {
            //Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
            let avatar = req.files.avatar;
            //Use the mv() method to place the file in the upload directory (i.e. "uploads")
  './uploads/' +;

            //send response
                status: true,
                message: 'File is uploaded',
                data: {
                    mimetype: avatar.mimetype,
                    size: avatar.size
    } catch (err) {

The above code snippet is an HTTP POST function. When you send a multipart/form-data request to the /upload-avatar route to upload a file, this function saves the file to the uploads folder on the server.

How does express-fileupload middleware work?

It makes the uploaded files accessible from the req.files property. For example, if you are uploading a file called my-profile.jpg, and your field name is avatar, you can access it via req.files.avatar. The avatar object will contain the following information:

  • - The name of the uploaded file i.e. my-profile.jpg
  • - The function to move the file elsewhere on the server
  • avatar.mimetype - The mime-type of the file
  • avatar.size - The size of the file in bytes
  • - A buffer representation of the uploaded file

Upload Multiple Files

Let us start creating another route to allow users to upload multiple photos in one request.'/upload-photos', async (req, res) => {
    try {
        if(!req.files) {
                status: false,
                message: 'No file uploaded'
        } else {
            let data = []; 
            //loop all files
            _.forEach(_.keysIn(, (key) => {
                let photo =[key];
                //move photo to uploads directory
      './uploads/' +;

                //push file details
                    mimetype: photo.mimetype,
                    size: photo.size
            //return response
                status: true,
                message: 'Files are uploaded',
                data: data
    } catch (err) {

The above code is relatively similar to a single file upload, except that we now receive a field photos instead of avatar. We used lodash utility functions ( _.forEach() & _.keysIn()) to loop over the photos field and save each photo to uploads directory.

Testing the Application

We're almost done! Run the following command in your terminal from the root directory of the project to start the application:

$ node index.js 

It will start the application at port 3000. Let us use Postman for sending HTTP multipart/form-data requests:

1. Single File

Upload Single File

2. Multiple Files

Upload Multiple Files

If you want to make the uploaded files publicly accessible from anywhere, just make the uploads directory static:


Now, you can open the uploaded file directly in the browser:


File Size Limit

If you want to limit the size of the files uploaded at once, pass the limits options directly to the express-fileupload middleware:

    createParentPath: true,
    limits: { 
        fileSize: 2 * 1024 * 1024 * 1024 //2MB max file(s) size

Source code: Download the complete source code from GitHub available under MIT license.


That's all folks! We learn how to upload single and multiple files using Node.js and the Express framework. The express-fileupload library is an easy-to-use Express middleware for handling file(s) upload. Read its documentation for more configuration options.

Read Next: Express File Upload with Multer in Node.js

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.