Working with File Systems -

Himanshu MauryaHimanshu Maurya
5 min read

A file system is like a smart organizer for your computer. It helps the computer store files (like photos, videos, documents) and keep them in order so you can easily find, open, save, or delete them.

The file system helps the computer:

  • Create new files

  • Read and open files

  • Save changes to files

  • Delete files

  • Keep files in folders (called directories)

Uses of File System

1. Saving Uploaded Files

  • For handling file uploads (images, PDFs, etc.) using Multer or similar tools.

2. Logging

  • Storing logs like errors, user actions, API responses in .log or .txt files.

3. Temporary File Storage

  • Storing temporary data like user sessions, caching API results, or intermediate file processing.

4. Desktop Applications

  • Apps like note-taking tools that save data locally.

5. Generating and Storing Reports

  • Create .csv or .pdf reports and store them on disk for download.

6. Configuration or Metadata Storage

  • Saving configs like .json, .env, or .yaml when small apps don't need a database.

Before learning how Multer works, we first need to understand how the traditional fs (File System) module works.

The fs module in Node.js is used to read, write, create, and delete files on your computer. It helps manage files manually without using any special middleware like Multer. Once we understand this, it becomes easier to see how Multer makes file handling simpler and better.

Using fs Module in Nodejs to read and Write files

The fs module in Node.js allows you to interact with the file system – reading, writing, updating, deleting files, and more. It’s built-in, so no need to install anything.

const fs = require('fs');

Writing to a File

fs.writeFile (Async)

const fs = require('fs');

fs.writeFile('example.txt', 'Hello, this is a write example!', (err) => {
  if (err) {
    console.error('Error writing file:', err);
    return;
  }
  console.log('File written successfully!');
});

fs.writeFileSync (Sync)

const fs = require('fs');

try {
  fs.writeFileSync('example.txt', 'Hello, this is a synchronous write!');
  console.log('File written successfully!');
} catch (err) {
  console.error('Error writing file:', err);
}

Reading from a File

fs.readFile (Async)

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  console.log('File contents:', data);
});

fs.readFileSync (Sync)

const fs = require('fs');

try {
  const data = fs.readFileSync('example.txt', 'utf8');
  console.log('File contents:', data);
} catch (err) {
  console.error('Error reading file:', err);
}
  • Use async methods for better performance in large applications.

  • Use sync methods in simple scripts or when blocking is acceptable.

Handling File uploads in Express with Multer

Handling file uploads in Express with Multer is a common practice in Node.js applications. Multer is a middleware for handling multipart/form-data .

Step-by-Step Guide to Handle File Uploads with Multer -

1.Install Required Packages

npm install express multer

2.Basic Setup

Create a file app.js

const express = require('express');
const multer = require('multer');
const path = require('path');

const app = express();

// Set storage engine
const storage = multer.diskStorage({
  destination: './uploads/', // folder to save files
  filename: (req, file, cb) => {
    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  }
});

// Init upload
const upload = multer({
  storage: storage,
  limits: { fileSize: 1000000 }, // Limit file size to 1MB
  fileFilter: (req, file, cb) => {
    const fileTypes = /jpeg|jpg|png|gif/;
    const extname = fileTypes.test(path.extname(file.originalname).toLowerCase());
    const mimetype = fileTypes.test(file.mimetype);
    if (extname && mimetype) {
      return cb(null, true);
    } else {
      cb('Error: Images Only!');
    }
  }
}).single('myImage'); // 'myImage' is the name of the form field

// Route
app.post('/upload', (req, res) => {
  upload(req, res, (err) => {
    if (err) {
      res.status(400).send({ message: err });
    } else {
      if (req.file == undefined) {
        res.status(400).send({ message: 'No file selected!' });
      } else {
        res.send({
          message: 'File uploaded successfully!',
          filename: req.file.filename,
          path: `/uploads/${req.file.filename}`
        });
      }
    }
  });
});

// Start server
app.listen(3000, () => console.log('Server started on port 3000'));

3.Create Uploads Folder

Create a folder called uploads in the root of your project to store uploaded files:

mkdir uploads

4.Test Upload

You can test the upload using:

  • Postman (set method to POST and use "form-data" to send a file),

  • Or use a frontend form like:

<form ref="uploadForm" 
      id="uploadForm" 
      action="http://localhost:3000/upload" 
      method="post" 
      encType="multipart/form-data">
      <input type="file" name="myImage" />
      <input type="submit" value="Upload!" />
</form>

Notes

  • upload.single('fieldname'): for uploading a single file

  • upload.array('fieldname', maxCount): for multiple files

  • upload.fields([{ name: 'avatar' }, { name: 'gallery' }]): for mixed file fields

Summary
Creating a file uploader using Express and Multer is a simple and effective way to handle file uploads in a Node.js app. Multer makes it easy to upload one or more files, set limits on file size and type, and save files safely on the server. Whether you’re building a feature to upload profile pictures or documents, this method gives you a strong and secure setup to manage file uploads smoothly.

File handling flow: client -> upload -> server -> save

1. Client (Frontend)

  • User selects a file using an <input type="file"> element.

  • On form submission, the file is sent to the server via a POST request (often with multipart/form-data encoding).

      <form action="/upload" method="POST" enctype="multipart/form-data">
        <input type="file" name="myFile" />
        <button type="submit">Upload</button>
      </form>
    

2. Upload (HTTP Request)

  • The browser sends the file as a part of the request body.

  • The request includes metadata like file name, type, and size.

3. Server (Backend)

  • The server (e.g., Express with Multer in Node.js) receives the request.

  • Middleware like multer parses the file from the request.

const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('myFile'), (req, res) => {
  console.log(req.file); // metadata about uploaded file
  res.send('File uploaded successfully');
});

4. Save (File Storage)

  • The file is saved to disk or cloud (like AWS S3, Cloudinary).

  • You can rename, move, or process the file as needed.

  • Optionally, store file info in a database.

Summary
Working with files is an important part of many programs. Using Node.js and the fs module, you can easily read, write, append, delete, and manage files and folders. It's better to use the asynchronous methods because they don’t stop your app while doing the task. Overall, the file system in Node.js makes it simple to work with files in your projects.

I’m truly thankful for your time and effort in reading this.

10
Subscribe to my newsletter

Read articles from Himanshu Maurya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Maurya
Himanshu Maurya

Hi, Thank-you for stopping by and having a look at my profile. Hi! I’m a web developer who loves working with the MERN stack . I enjoy making interactive and user-friendly websites and webapps. I’m great at taking ideas and bringing them to life through coding!