Node file upload with the express js

Learn Node JS file upload With Express JS Framework

Hi, in this Node file upload tutorial you will learn how to upload file using Express JS framework.

To upload files we will use the express-fileupload module and the multer module.


Upload files using express-fileupload

First, install the below node dependencies in your express app –

express and express-fileuploadnpm install express express-fileuploa --save

Before we start creating files and folders, let’s take a look at the app folder structure –

Express Js file upload app folder structure

Now you need to create two folders one is uploads another is temp inside our app.

After that we will create index.js file –

index.js

const express = require('express');
const expressFileUpload = require('express-fileupload');
const path = require('path');
const app = express();

// applying expressFileUpload middleware
app.use(expressFileUpload({
    useTempFiles: true,
    tempFileDir: path.join(__dirname, 'temp')
}));

app.get('/', (req, res) => {
    res.send(`
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="target_file" required>
    <input type="submit" value="Upload">
</form>
    `);
});

app.post('/', (req, res) => {
    // checking req.files is empty or not
    if (Object.keys(req.files).length == 0) {
        return res.status(400).send('No files were uploaded.');
    }

    let target_file = req.files.target_file;

    // target_file.mv(path, callback)
    target_file.mv(path.join(__dirname, 'uploads', target_file.name), (err) => {
        if (err) throw err;
        res.send('File Uploaded');
    })

})

app.listen(3000, () => console.log('Your app listening on port 3000'));

Access upload file

<input name="target_file" type="file" />
req.files.target_file


The req.files.target_file object will contain the following:

  • req.files.target_file.name: Access the file name (example.png)
  • req.files.target_file.mv: A function to move the file elsewhere on your server
  • req.files.target_file.mimetype: The mimetype of your file
  • req.files.target_file.data: A buffer representation of your file, returns empty buffer in case useTempFiles option was set to true
  • req.files.target_file.tempFilePath: A path to the temporary file in case useTempFiles option was set to true
  • req.files.target_file.truncated: A boolean that represents if the file is over the size limit
  • req.files.target_file.size: Size of the file in bytes
  • req.files.target_file.md5: MD5 checksum of the uploaded file

How to check the file already exists or not

use the file system(fs) module If you want to check that the upload file already exists or not.

fs.exists('file path', callback)

Click here to learn more about the express-fileupload module.


Upload files using multer

There are too many node js modules to use for uploading files. But the multer is the most popular module to use for uploading files.

Learn Multer


Next is – How to Validate form data Using Express JS

Previous isExpresss JS Session

Leave a Reply

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