Node.js Crash Course Tutorial #2 - Node.js Basics
Table of Contents
Introduction
This tutorial provides a comprehensive overview of Node.js basics, including how to run JavaScript files on your computer, understand the global object, manage modules, utilize the file system, and work with streams. This knowledge is essential for developing server-side applications and understanding Node.js functionality.
Chapter 1: Node Basics
To begin using Node.js on your computer:
- Open a Terminal: This can be an integrated terminal in VS Code or a command prompt.
- Run a JavaScript File: Create a JavaScript file (e.g.,
test.js
) and execute it using the command:node test.js
- Write JavaScript Code: For example, create a simple greeting function:
function greet(name) { console.log(`Hello, ${name}`); } greet('Mario'); greet('Jasha');
- Execute Again: Run the file again to see the output.
Chapter 2: The Global Object
In Node.js, the global object differs from the browser's window
object.
- Access Global Object: Use
global
instead ofwindow
. - Log the Global Object: Create a file (e.g.,
global.js
) and log the global object:console.log(global);
- Use Global Methods: For instance, using
setTimeout
:global.setTimeout(() => { console.log('Timeout executed'); }, 3000);
- Access Directory and File Names:
- Use
__dirname
for the directory path. - Use
__filename
for the file path.
console.log(__dirname); console.log(__filename);
- Use
Chapter 3: Modules and Require
Node.js allows you to split code into multiple files for better organization.
- Create Module Files: Create
people.js
with:const people = ['Mario', 'Jasha']; console.log(people); module.exports = people;
- Import the Module: In another file (e.g.,
modules.js
):const people = require('./people'); console.log(people);
- Export Multiple Items: You can export an object for multiple exports:
module.exports = { people, ages: [25, 30] };
- Destructure Imports:
const { people, ages } = require('./people');
Chapter 4: Node and the File System
Node.js provides the fs
module to interact with the file system.
- Import the File System Module:
const fs = require('fs');
- Read Files: Use
fs.readFile
:fs.readFile('./docs/blog1.txt', 'utf8', (err, data) => { if (err) { console.log(err); return; } console.log(data); });
- Write Files: Use
fs.writeFile
:fs.writeFile('./docs/blog1.txt', 'New content', (err) => { if (err) { console.log(err); return; } console.log('File written'); });
- Create and Delete Directories:
- Create a directory:
fs.mkdir('./assets', (err) => { if (err) { console.log(err); return; } console.log('Directory created'); });
- Delete a directory:
fs.rmdir('./assets', (err) => { if (err) { console.log(err); return; } console.log('Directory deleted'); });
- Create a directory:
Chapter 5: Streams and Buffers
Streams allow you to read large files efficiently without waiting for the entire file to load.
- Create a Read Stream:
const readStream = fs.createReadStream('./docs/blog3.txt', { encoding: 'utf8' }); readStream.on('data', (chunk) => { console.log('New chunk received:', chunk); });
- Create a Write Stream:
const writeStream = fs.createWriteStream('./docs/blog4.txt'); writeStream.write('Hello World\n'); writeStream.write('New chunk\n');
- Use Pipe to Stream Data:
readStream.pipe(writeStream);
Conclusion
In this tutorial, you learned the fundamentals of Node.js, including how to run JavaScript files, utilize the global object, manage modules, interact with the file system, and handle streams. These concepts form the foundation for building server-side applications with Node.js. Next, consider exploring the HTTP module to create servers or dive deeper into Node.js features for more advanced applications.