File Streaming and Automation with Node.js
Overview
This assignment focused on working with file streams in Node.js. Streams are commonly used in programming to efficiently handle large amounts of data without loading everything into memory at once. Understanding how streams work is useful in many areas of computing including server applications, data processing, and cybersecurity tools.
In this project I learned how to read data from one file and write it to another file using Node.js. The program also demonstrates how command line arguments work and how developers can build simple tools that automate file operations.
Working with file streams helped me better understand how applications process data behind the scenes and how programming can be used to automate repetitive tasks.
Artifact: Node.js File Streaming Program
The goal of this assignment was to create a program that copies the contents of one file to another file using streams. The script is run from the command line and requires the user to provide a source file and a destination file.
The program reads the contents of the source file and writes them to the destination file. It also checks for errors in case the file does not exist.
// STEP 1: ADD DEPENDENCIES
const fs = require('fs');
const path = require('path');
// STEP 2: VERIFY THE CORRECT INVOCATION OF THE SCRIPT
if (process.argv.length !== 4) {
console.log(`Usage: node ${path.basename(process.argv[1])} [source] [destination]`);
return;
}
// STEP 3: EXTRACT THE SOURCE FILE NAME AND THE DESTINATION FILE NAME
const source = process.argv[2];
const dest = process.argv[3];
// STEP 4: CREATE A READABLE STREAM FROM THE SOURCE FILE
const rStream = fs.createReadStream(source);
// STEP 5: CREATE A WRITABLE STREAM TO THE DESTINATION FILE
const wStream = fs.createWriteStream(dest);
// STEP 6: USING A PIPE ATTACH THE READABLE STREAM TO THE WRITABLE STREAM
rStream.pipe(wStream);
// STEP 7: LISTEN FOR ERRORS ON THE READABLE STREAM
rStream.on('error', () => {
console.log('File not found');
});
This program shows how Node.js streams allow data to move efficiently from one place to another without requiring the entire file to be loaded at once.
Reflection
This assignment helped me better understand how Node.js handles file operations and how developers can build tools that interact with the operating system. Writing the script also helped me become more comfortable using the command line and working with program arguments.
Learning about streams is important because many applications rely on them to process data efficiently. In cybersecurity and IT environments, tools often need to handle large files such as logs or system data. Understanding how file streams work can help when developing scripts that analyze or process that information.
Overall this project helped strengthen my programming skills and gave me more experience working with Node.js and file system operations.