Node.js Automation: File Handling and Automatic Image Downloading

![[Node.JS 자동화] Node.js 자동화 프로그래밍, 반복 작업을 AI와 함께 자동화하는 실전 개발(프롤로그)](/uploads/editor/Node-JS-%EC%9E%90%EB%8F%99%ED%99%94-Node-js-%EC%9E%90%EB%8F%99%ED%99%94-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-%EB%B0%98%EB%B3%B5-%EC%9E%91%EC%97%85%EC%9D%84-AI%EC%99%80-%ED%95%A8%EA%BB%98-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%98%EB%8A%94-%EC%8B%A4%EC%A0%84-%EA%B0%9C%EB%B0%9C-%ED%94%84%EB%A1%A4%EB%A1%9C%EA%B7%B8-20260702-e78eab.webp)
Why Is File Handling Important?
Automation programs often deal with a large number of files.
For example
Saving AI-generated content
Storing news articles
Downloading images
Creating log files
Saving JSON data
Generating CSV files
Backing up files
Most automation programs require the ability to read and save files.
In Node.js, this functionality is handled by the fs (File System) module.
The fs Module Explained
fs is a built-in module in Node.js for file processing.
No additional installation is required to use it.
First, import the fs module in your project.
const fs = require("fs");
This single line gives you access to all file-related operations like reading, writing, deleting, and creating folders.
Reading Files
The simplest way to read a text file.
const fs = require("fs");
const text = fs.readFileSync("sample.txt", "utf8");
console.log(text);
The file content is output as a string.
This method is commonly used when reading configuration files or JSON files.
Reading Asynchronously
In practice, asynchronous methods are preferred.
fs.readFile("sample.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
This approach allows the program to continue other tasks while reading the file, making it more efficient.
Writing Files
Saving files is straightforward.
fs.writeFileSync(
"result.txt",
"Node.js automation course."
);
If the file doesn't exist, it's created. If it does exist, it's overwritten.
Asynchronous Saving
In practice, writeFile is often used asynchronously as well.
fs.writeFile(
"result.txt",
"Auto-saved.",
(err) => {
if (err) {
console.error(err);
}
}
);
This is especially efficient when dealing with large data.
Saving JSON Files
Automation programs frequently save JSON files.
For example:
const data = {
title: "Node.js",
category: "Course"
};
fs.writeFileSync(
"post.json",
JSON.stringify(data, null, 2)
);
This creates a human-readable JSON file.
Reading JSON Files
JSON files saved can be easily loaded back.
const json = JSON.parse(
fs.readFileSync("post.json", "utf8")
);
console.log(json.title);
This method is commonly used for managing configuration files in automation programs.
Creating Folders
To store images or logs, folders are needed.
if (!fs.existsSync("images")) {
fs.mkdirSync("images");
}
If the folder doesn't exist, it's automatically created.
This is one of the most commonly used code snippets in real-world projects.
Checking File Existence
Here's how to check if a file exists:
if (fs.existsSync("config.json")) {
console.log("File exists");
}
It's good practice to check for file existence before attempting to read it, avoiding errors.
Automatic Image Downloading
Now let's create one of the most commonly used features: automatic image downloading.
First, install Axios:
npm install axios
Then download an image:
const axios = require("axios");
const fs = require("fs");
const response = await axios({
url: imageUrl,
method: "GET",
responseType: "stream"
});
response.data.pipe(
fs.createWriteStream("images/photo.jpg")
);
This automatically saves images from the internet.
Automatically Generating File Names
In automation programs, file names are often generated automatically.
For example:
const filename = Date.now() + ".jpg";
Or:
const filename = postId + ".webp";
This helps prevent duplicate file names.
Working with Paths
It's recommended to use the path module for handling file paths.
const path = require("path");
const filepath = path.join(
__dirname,
"images",
"photo.jpg"
);
This ensures paths are created safely across different operating systems.
Handling Existing Images
In real-world projects, duplicate downloads are avoided.
if (!fs.existsSync(filepath)) {
// Download image
}
This prevents unnecessary network traffic and server load when images have already been downloaded.
How File Handling Is Used in Automation Projects
In the project we're building, file handling will be used extensively.
For example:
Saving AI-generated content
Downloading images
Saving thumbnails
Storing logs
Creating JSON backups
Generating CSV files
<Managing configuration files
This is applicable to almost all projects.
Automatic Image Storage System
An AI auto-posting system works as follows:
Keyword generation
↓
AI content creation
↓
Image generation
↓
Image downloading
↓
WebP conversion
↓
Thumbnail generation
↓
Post publishing
The file handling techniques we've learned form the core of this process.
File Management Is a Habit
As projects grow, so do the number of files.
Therefore, it's beneficial to organize files into purpose-specific folders like:
images
uploads
downloads
logs
backup
This makes maintenance much easier later on.
Performance Considerations
When handling large files, using a stream-based approach is better than loading everything into memory.
In Axios, you can use:
responseType: "stream"
This allows downloading large images or videos without excessive memory usage.
In practice, stream-based approaches are almost always used in real-world projects.
Conclusion
In this lesson, we've covered how to read and save files in Node.js, as well as how to automatically download images from the internet.
File handling is a core skill that cannot be omitted in automation programs.
We'll continue using these techniques in our upcoming AI auto-posting system for saving and managing AI-generated content and images.
In the next lesson, we'll learn how to create a Telegram notification bot to receive real-time updates on your automation program's execution results, including error messages and completion notifications.
FAQ
What is the fs module in Node.js?
The fs (File System) module is a built-in Node.js module for handling file operations like reading, writing, and deleting files without requiring additional installation.
Why use asynchronous methods for file operations?
Asynchronous methods allow the program to continue executing other tasks while waiting for file operations to complete, improving efficiency especially with larger files or network requests.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 자동화] 10강. 파일 읽기·쓰기와 이미지 자동 다운로드