Module 1: Core Node.js Fundamentals - Understanding the Engine
Theoretical Concepts:
global, console, process, Buffer, setTimeout, setInterval, etc.require() vs import: Understanding the historical context and modern usage.Code Samples:
JavaScript
// CommonJS (default in older Node.js versions)
const fs = require('fs');
// ES Modules (requires configuration in package.json)
import fs from 'fs/promises';
console.log('Hello from Node.js');
console.log(process.env.NODE_ENV);
fs.readFile('myfile.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Hands-on Assignments:
Checkpoint 1: You should understand the core principles of Node.js architecture, the event loop, and how to work with basic global objects and the module system.
Module 2: Asynchronous Programming - Mastering Non-Blocking Operations
Theoretical Concepts:
.then(), .catch(), and .finally()..catch()), and async/await (using try/catch).Code Samples:
JavaScript
// Callback
fs.readFile('data.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('Data:', data);
});
// Promise
fs.promises.readFile('data.txt', 'utf8')
.then(data => console.log('Data (Promise):', data))
.catch(err => console.error('Error (Promise):', err));
// async/await
async function readFileAsync() {
try {
const data = await fs.promises.readFile('data.txt', 'utf8');
console.log('Data (async/await):', data);
} catch (err) {
console.error('Error (async/await):', err);
}
}
readFileAsync();
Hands-on Assignments:
Checkpoint 2: You should be proficient in using callbacks, Promises, and async/await for managing asynchronous operations and implementing proper error handling.
Module 3: File System & Path Modules - Interacting with the System
Theoretical Concepts:
fs.readFileSync, fs.writeFileSync, fs.readFile, fs.writeFile, fs.promises.readFile, fs.promises.writeFile).fs.mkdir, fs.readdir, fs.rename, fs.rmdir, fs.promises.mkdir, etc.).path.join, path.resolve, path.dirname, path.basename, path.extname).Code Samples:
JavaScript
import fs from 'fs/promises';
import path from 'path';
async function fileOperations() {
const filePath = path.join(__dirname, 'mydata.txt');
const dirPath = path.join(__dirname, 'new_directory');
try {
await fs.writeFile(filePath, 'Hello, Node.js!', 'utf8');
const data = await fs.readFile(filePath, 'utf8');
console.log('File content:', data);
await fs.mkdir(dirPath, { recursive: true });
console.log('Directory created:', dirPath);
const files = await fs.readdir(__dirname);
console.log('Files in current directory:', files);
} catch (err) {
console.error('File operation error:', err);
}
}
fileOperations();
const fileName = '/users/documents/report.pdf';
console.log('Directory name:', path.dirname(fileName));
console.log('Base name:', path.basename(fileName));
console.log('Extension name:', path.extname(fileName));
Hands-on Assignments:
Checkpoint 3: You should be comfortable reading and writing files, working with streams for efficient data handling, managing directories, and manipulating file paths using the fs and path modules.
Module 4: HTTP Module & Web Servers - Building the Foundation of Web Applications
Theoretical Concepts:
http module to create a basic web server.Code Samples:
JavaScript
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!\\n');
});
const port = 3000;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
JavaScript
// Simple routing logic
const serverWithRouting = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/home') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome Home!</h1>');
} else if (req.method === 'POST' && req.url === '/api/data') {
let body = '';
req.on('data', chunk => {
body += chunk;
});
req.on('end', () => {
console.log('Received data:', body);
res.writeHead(201, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Data received successfully' }));
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found\\n');
}
});
Hands-on Assignments:
Checkpoint 4: You should be able to build custom HTTP servers using the http module, implement basic routing, and handle requests and responses.