๐Ÿ—‚๏ธ 3.8 Working with File Systems in Node.js โ€” Reading, Writing & Uploading Files Made Easy

Table of Contents

Hey there, lil’ coder! ๐Ÿ‘‹

Ever wanted your Node.js app to read from or write to a file? Or maybe let users upload images and documents?

Thatโ€™s exactly what weโ€™ll learn today! ๐Ÿ˜Ž
Weโ€™ll use simple language, fun code, and real examples โ€” just like Iโ€™m teaching my younger cousin ๐Ÿ’ก


๐Ÿ“ฆ What Is the File System in Node.js?

Every computer has a file system โ€” a way to organize and manage files and folders.

Node.js gives us a cool tool to play with files: the fs module (short for File System).

This module lets us:

โœ… Read files
โœ… Write files
โœ… Update files
โœ… Delete files
โœ… And even upload files from users!


๐Ÿ“š How to Use the fs Module in Node.js

Letโ€™s get started by creating a Node.js project:

bashCopyEditmkdir file-system-app
cd file-system-app
npm init -y

Create a file:

bashCopyEdittouch index.js

Now, inside index.js, weโ€™ll start using the built-in fs module.

jsCopyEditconst fs = require('fs');

๐ŸŽ‰ That’s it! Now we can start reading and writing files.


๐Ÿ“– 1. Reading Files in Node.js

There are 2 ways to read files:

๐Ÿ”„ Synchronous (Blocking) โ€“ fs.readFileSync

jsCopyEditconst data = fs.readFileSync('example.txt', 'utf-8');
console.log(data);
  • This stops the program until the file is read.
  • Use it for small scripts.

๐Ÿ” Asynchronous (Non-blocking) โ€“ fs.readFile

jsCopyEditfs.readFile('example.txt', 'utf-8', (err, data) => {
  if (err) return console.error(err);
  console.log(data);
});
  • Doesnโ€™t block the rest of your app.
  • Best for web servers or large files.

๐Ÿ“ฆ Letโ€™s Create a Test File

Create a file called example.txt:

perlCopyEditThis is my first file read in Node.js ๐Ÿš€

Now run your script. Youโ€™ll see the message in your terminal!

๐Ÿง  Tip: Always use async methods in real apps. Theyโ€™re faster and safer!


โœ๏ธ 2. Writing Files in Node.js

You can create or replace a file using fs.writeFile:

jsCopyEditfs.writeFile('newFile.txt', 'Hello from Web Codder! ๐Ÿงก', (err) => {
  if (err) throw err;
  console.log('โœ… File written successfully!');
});
  • If the file doesnโ€™t exist, Node creates it.
  • If it exists, the content gets replaced.

โž• 3. Appending Data to a File

Want to add more data to an existing file? Use fs.appendFile:

jsCopyEditfs.appendFile('newFile.txt', '\nAnother line from Web Codder ๐Ÿ”ฅ', (err) => {
  if (err) throw err;
  console.log('โœ… Data appended!');
});

๐Ÿ” \n adds a new line between entries.


๐Ÿงจ 4. Deleting a File

Want to remove a file?

jsCopyEditfs.unlink('newFile.txt', (err) => {
  if (err) throw err;
  console.log('๐Ÿ—‘๏ธ File deleted!');
});

Be careful โ€” once deleted, itโ€™s gone! ๐Ÿ˜…


๐Ÿงบ 5. Handling File Uploads with Multer

Now letโ€™s make it fancy ๐ŸŽฉ

Imagine your app lets users upload profile pictures.
Youโ€™ll need a way to receive and save uploaded files.

Here comes multer โ€” a popular middleware for handling file uploads in Express apps.

๐Ÿ“ฆ Install multer:

bashCopyEditnpm install express multer

๐Ÿ› ๏ธ Basic File Upload Setup

jsCopyEditconst express = require('express');
const multer = require('multer');
const app = express();

const storage = multer.diskStorage({
  destination: (req, file, cb) => cb(null, 'uploads/'),
  filename: (req, file, cb) => cb(null, Date.now() + '-' + file.originalname)
});

const upload = multer({ storage });

app.post('/upload', upload.single('myFile'), (req, res) => {
  res.send('โœ… File uploaded successfully!');
});

app.listen(3000, () => console.log('Server running on port 3000'));

๐Ÿ“ Folder Structure After Upload

pgsqlCopyEditfile-system-app/
โ”œโ”€โ”€ index.js
โ”œโ”€โ”€ uploads/
โ”‚   โ””โ”€โ”€ 1715087731873-profile.jpg

๐Ÿง  Multer saves your uploaded files to the uploads/ folder.

File Upload Vuln Pic 4
๐Ÿ—‚๏ธ 3.8 Working With File Systems In Node.js โ€” Reading, Writing & Uploading Files Made Easy 2


โœ… Recap โ€” What You Learned Today

ConceptCode Example
Read File (Sync)fs.readFileSync()
Read File (Async)fs.readFile()
Write Filefs.writeFile()
Append Filefs.appendFile()
Delete Filefs.unlink()
Upload Filemulter.upload.single()

Youโ€™re now a file-system ninja! ๐Ÿฅท


๐Ÿ› ๏ธ Project Ideas to Practice

  • ๐Ÿ““ Build a simple Notepad that writes to .txt files
  • ๐Ÿ“ท Image gallery where users upload and view pictures
  • ๐Ÿ“‚ Backup tool that saves server logs daily

๐Ÿ“ฃ Stay Connected with Web Codder

Want more awesome tutorials and beginner-friendly guides?

๐Ÿ“บ Subscribe to Web Codder on YouTube
๐Ÿ“ธ Follow us on Instagram
๐Ÿ’ฌ Join our WhatsApp Community

Letโ€™s keep learning and building together! ๐Ÿ’ก๐Ÿ’ป

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts