HTML & JS Topic
Node.js Core Built-in Modules
1. fs (File System) Module
The fs module provides file system operations (reading, writing, updating, deleting files/directories).
Synchronous vs Asynchronous vs Promise-based APIs:
javascript
const fs = require('fs');
const fsPromises = require('fs/promises'); // Node.js v14+
// Synchronous (blocking) - use carefully!
try {
const data = fs.readFileSync('file.txt', 'utf8');
console.log(data);
} catch (err) {
console.error(err);
}
// Asynchronous (callback-based) - traditional
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log(data);
});
// Promise-based (modern, recommended)
async function readFile() {
try {
const data = await fsPromises.readFile('file.txt', 'utf8');
console.log(data);
} catch (err) {
console.error(err);
}
}
Common fs Operations:
File Operations:
javascript
// Read file
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err;
console.log(JSON.parse(data));
});
// Write file (overwrites if exists)
fs.writeFile('output.txt', 'Hello World', 'utf8', (err) => {
if (err) throw err;
console.log('File written');
});
// Append to file
fs.appendFile('log.txt', '\nNew log entry', (err) => {
if (err) throw err;
console.log('Appended to file');
});
// Check if file exists
fs.access('file.txt', fs.constants.F_OK, (err) => {
console.log(err ? 'File does not exist' : 'File exists');
});
// Get file stats
fs.stat('file.txt', (err, stats) => {
if (err) throw err;
console.log(`Size: ${stats.size} bytes`);
console.log(`Is file: ${stats.isFile()}`);
console.log(`Is directory: ${stats.isDirectory()}`);
console.log(`Created: ${stats.birthtime}`);
console.log(`Modified: ${stats.mtime}`);
});
// Copy file
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) throw err;
console.log('File copied');
});
// Rename/Move file
fs.rename('oldname.txt', 'newname.txt', (err) => {
if (err) throw err;
console.log('File renamed');
});
// Delete file
fs.unlink('file.txt', (err) => {
if (err) throw err;
console.log('File deleted');
});
// Watch file for changes
const watcher = fs.watch('file.txt', (eventType, filename) => {
console.log(`Event type: ${eventType}`);
console.log(`Filename: ${filename}`);
});
// Stop watching after 10 seconds
setTimeout(() => watcher.close(), 10000);
Directory Operations:
javascript
// Create directory
fs.mkdir('new-folder', { recursive: true }, (err) => {
if (err) throw err;
console.log('Directory created');
});
// Read directory contents
fs.readdir('./', (err, files) => {
if (err) throw err;
files.forEach(file => console.log(file));
});
// Remove directory
fs.rmdir('empty-folder', (err) => {
if (err) throw err;
console.log('Directory removed');
});
// Remove directory recursively (Node.js v14.14+)
fs.rm('folder-with-content', { recursive: true, force: true }, (err) => {
if (err) throw err;
console.log('Directory removed recursively');
});
Streams for Large Files:
javascript
// Read large file as stream
const readStream = fs.createReadStream('large-file.txt', 'utf8');
readStream.on('data', (chunk) => {
console.log('Received chunk:', chunk.length, 'bytes');
});
readStream.on('end', () => console.log('Finished reading'));
// Write large file as stream
const writeStream = fs.createWriteStream('output.txt');
writeStream.write('Line 1\n');
writeStream.write('Line 2\n');
writeStream.end();
// Pipe streams (copy file)
const source = fs.createReadStream('source.txt');
const dest = fs.createWriteStream('destination.txt');
source.pipe(dest);
2. path Module
Handles and transforms file paths in a cross-platform way.
javascript
const path = require('path');
Basic Path Operations:
javascript
// Join path segments
console.log(path.join('/foo', 'bar', 'baz')); // /foo/bar/baz
console.log(path.join('dir', '..', 'file.txt')); // file.txt
console.log(path.join('foo', '//bar', 'baz//')); // foo/bar/baz (normalized)
// Resolve to absolute path
console.log(path.resolve('src', 'app.js')); // /full/path/src/app.js
console.log(path.resolve('/foo/bar', './baz')); // /foo/bar/baz
console.log(path.resolve('/foo/bar', '/tmp/file')); // /tmp/file
console.log(path.resolve('wwwroot', 'static/img')); // /current/dir/wwwroot/static/img
// Get directory name
console.log(path.dirname('/foo/bar/baz.txt')); // /foo/bar
// Get file name
console.log(path.basename('/foo/bar/baz.txt')); // baz.txt
console.log(path.basename('/foo/bar/baz.txt', '.txt')); // baz (remove extension)
// Get file extension
console.log(path.extname('index.html')); // .html
console.log(path.extname('file.min.js')); // .js
console.log(path.extname('.gitignore')); // '' (no extension)
Path Parsing and Formatting:
javascript
// Parse path into components
const parsed = path.parse('/home/user/file.txt');
console.log(parsed);
// {
// root: '/',
// dir: '/home/user',
// base: 'file.txt',
// ext: '.txt',
// name: 'file'
// }
// Format object back to path
const formatted = path.format({
dir: '/home/user',
name: 'file',
ext: '.txt'
});
console.log(formatted); // /home/user/file.txt
Working with Relative Paths:
javascript
// Check if path is absolute
console.log(path.isAbsolute('/foo/bar')); // true
console.log(path.isAbsolute('foo/bar')); // false
console.log(path.isAbsolute('C:\\foo')); // true (Windows)
// Get relative path between two paths
console.log(path.relative('/data/orandea/test', '/data/orandea/impl')); // ../impl
console.log(path.relative('/a/b/c', '/a/d/e')); // ../../d/e
// Normalize path (remove . and .., resolve //, etc.)
console.log(path.normalize('/foo/bar//baz/../qux')); // /foo/bar/qux
console.log(path.normalize('C:\\temp\\\\foo\\bar\\..\\')); // C:\temp\foo\
Platform-Specific Paths:
javascript
// Platform-specific path segment separator
console.log(path.sep); // '/' on Unix, '\' on Windows
// Platform-specific delimiter for PATH variable
console.log(path.delimiter); // ':' on Unix, ';' on Windows
// Platform-specific methods
const winPath = path.win32.join('C:', 'Users', 'file.txt');
const posixPath = path.posix.join('/home', 'user', 'file.txt');
Practical Examples:
javascript
// Get file extension and name
function getFileInfo(filepath) {
return {
filename: path.basename(filepath, path.extname(filepath)),
extension: path.extname(filepath),
directory: path.dirname(filepath),
fullPath: path.resolve(filepath)
};
}
// Create safe file path
function createSafePath(userInput) {
// Sanitize user input for path
return path.join('uploads', path.basename(userInput));
}
// Working with __dirname and __filename
console.log('Current directory:', __dirname);
console.log('Current file:', __filename);
console.log('Path to config file:', path.join(__dirname, 'config.json'));
3. os Module
Provides operating system-related utility methods.
javascript
const os = require('os');
System Information:
javascript
// Platform
console.log('Platform:', os.platform()); // 'darwin', 'win32', 'linux', 'freebsd'
console.log('Architecture:', os.arch()); // 'x64', 'arm', 'arm64', 'ia32'
console.log('Release:', os.release()); // Kernel version
console.log('Type:', os.type()); // 'Darwin', 'Windows_NT', 'Linux'
// Host information
console.log('Hostname:', os.hostname()); // Computer name
console.log('Home directory:', os.homedir()); // /Users/username
console.log('Temp directory:', os.tmpdir()); // /tmp or C:\Users\...\AppData\Local\Temp
// Line endings for current OS
console.log('EOL:', JSON.stringify(os.EOL)); // "\n" on Unix, "\r\n" on Windows
CPU Information:
javascript
// Get CPU information
const cpus = os.cpus();
console.log(`Number of CPUs: ${cpus.length}`);
cpus.forEach((cpu, index) => {
console.log(`CPU ${index}:`);
console.log(` Model: ${cpu.model}`);
console.log(` Speed: ${cpu.speed} MHz`);
console.log(` Times:`, cpu.times);
});
// CPU architecture endianness
console.log('Endianness:', os.endianness()); // 'LE' or 'BE'
Memory Information:
javascript
// Total system memory
console.log('Total memory:', formatBytes(os.totalmem()));
console.log('Free memory:', formatBytes(os.freemem()));
console.log('Used memory:', formatBytes(os.totalmem() - os.freemem()));
// Calculate memory usage percentage
const memoryUsage = ((os.totalmem() - os.freemem()) / os.totalmem()) * 100;
console.log(`Memory usage: ${memoryUsage.toFixed(2)}%`);
// Helper function to format bytes
function formatBytes(bytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(2)} ${units[unitIndex]}`;
}
Network Information:
javascript
// Network interfaces
const networkInterfaces = os.networkInterfaces();
console.log('Network Interfaces:');
for (const [interfaceName, interfaces] of Object.entries(networkInterfaces)) {
console.log(`\n${interfaceName}:`);
interfaces.forEach(iface => {
if (iface.family === 'IPv4' && !iface.internal) {
console.log(` ${iface.address} (${iface.netmask})`);
}
});
}
User Information:
javascript
// Current user info
const userInfo = os.userInfo();
console.log('User Info:');
console.log(' Username:', userInfo.username);
console.log(' Home directory:', userInfo.homedir);
console.log(' Shell:', userInfo.shell || 'N/A');
// UID/GID (Unix only)
console.log('User ID:', os.userInfo().uid); // -1 on Windows
console.log('Group ID:', os.userInfo().gid); // -1 on Windows
// System uptime
console.log('System uptime:', formatUptime(os.uptime()));
function formatUptime(seconds) {
const days = Math.floor(seconds / (24 * 3600));
const hours = Math.floor((seconds % (24 * 3600)) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${days}d ${hours}h ${minutes}m ${secs}s`;
}
System Load & Priority:
javascript
// System load average (1, 5, 15 minutes)
const loadAvg = os.loadavg();
console.log(`Load average (1, 5, 15 min): ${loadAvg.join(', ')}`);
// Get/set process priority
const priority = os.getPriority(); // Current process priority
console.log('Process priority:', priority);
// Set priority (range: -20 to 19, lower = higher priority)
os.setPriority(10);
Constants:
javascript
// Error codes
console.log('Error constants:', os.constants.errno);
// Signal constants (for process signals)
console.log('Signal constants:', os.constants.signals);
// Priority constants
console.log('Priority constants:', os.constants.priority);
4. events Module
Implements the EventEmitter pattern for handling asynchronous events.
javascript
const EventEmitter = require('events');
Basic EventEmitter Usage:
javascript
// Create an event emitter instance
const emitter = new EventEmitter();
// Register event listener
emitter.on('greet', (name) => {
console.log(`Hello ${name}!`);
});
// Emit event
emitter.emit('greet', 'Alice'); // Hello Alice!
emitter.emit('greet', 'Bob'); // Hello Bob!
Event Types and Methods:
javascript
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
// on() - Add listener for event
myEmitter.on('event', () => console.log('Event occurred'));
// once() - Listen for event only once
myEmitter.once('connect', () => console.log('Connected once'));
// emit() - Trigger event
myEmitter.emit('event'); // Event occurred
myEmitter.emit('connect'); // Connected once
myEmitter.emit('connect'); // Nothing happens (only once)
// removeListener() - Remove specific listener
const callback = () => console.log('Will be removed');
myEmitter.on('test', callback);
myEmitter.removeListener('test', callback);
// off() - Alias for removeListener (Node.js v10+)
myEmitter.off('test', callback);
// removeAllListeners() - Remove all listeners for event
myEmitter.removeAllListeners('event');
// PrependListener() - Add listener to beginning of listeners array
myEmitter.prependListener('event', () => console.log('First'));
// setMaxListeners() - Change max listeners limit (default: 10)
myEmitter.setMaxListeners(20);
Error Handling:
javascript
const errorEmitter = new EventEmitter();
// Always handle 'error' events
errorEmitter.on('error', (err) => {
console.error('Error occurred:', err.message);
});
// Unhandled 'error' events will crash the process
try {
errorEmitter.emit('error', new Error('Something went wrong'));
} catch (err) {
console.log('Error caught:', err.message);
}
// Or handle globally
process.on('uncaughtException', (err) => {
console.error('Uncaught exception:', err);
process.exit(1);
});
Event Emitter in Classes:
javascript
class Database extends EventEmitter {
constructor() {
super();
this.connected = false;
}
connect() {
// Simulate async connection
setTimeout(() => {
this.connected = true;
this.emit('connected', { time: new Date() });
}, 1000);
}
query(sql) {
if (!this.connected) {
this.emit('error', new Error('Not connected'));
return;
}
// Simulate query
setTimeout(() => {
const result = { data: 'query results' };
this.emit('queryResult', result);
}, 500);
}
}
// Usage
const db = new Database();
db.on('connected', (details) => {
console.log('Database connected at:', details.time);
db.query('SELECT * FROM users');
});
db.on('queryResult', (result) => {
console.log('Query result:', result.data);
});
db.on('error', (err) => {
console.error('Database error:', err.message);
});
db.connect();
Advanced Event Patterns:
javascript
// Event names as constants
const Events = {
CONNECT: 'connect',
DISCONNECT: 'disconnect',
MESSAGE: 'message',
ERROR: 'error'
};
class ChatService extends EventEmitter {
sendMessage(user, message) {
this.emit(Events.MESSAGE, { user, message, timestamp: new Date() });
}
}
// Pass arguments
emitter.on('data', (chunk1, chunk2, chunk3) => {
console.log('Received data:', chunk1, chunk2, chunk3);
});
emitter.emit('data', 'a', 'b', 'c');
// Async event listeners (Node.js 11+)
emitter.on('asyncEvent', async (data) => {
await someAsyncOperation(data);
console.log('Async operation complete');
});
5. http Module
Creates HTTP servers and makes HTTP requests.
javascript
const http = require('http');
Creating an HTTP Server:
javascript
// Basic HTTP server
const server = http.createServer((req, res) => {
// Request object
console.log('Method:', req.method);
console.log('URL:', req.url);
console.log('Headers:', req.headers);
// Response object
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(3000, 'localhost', () => {
console.log('Server running at http://localhost:3000/');
});
Handling Different Routes:
javascript
const server = http.createServer((req, res) => {
const { method, url } = req;
// Parse URL
const parsedUrl = new URL(url, `http://${req.headers.host}`);
const pathname = parsedUrl.pathname;
// Route handling
if (pathname === '/' && method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Home Page</h1>');
}
else if (pathname === '/about' && method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>About Page</h1>');
}
else if (pathname === '/api/users' && method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]));
}
else if (pathname === '/api/users' && method === 'POST') {
// Handle POST request
let body = '';
req.on('data', chunk => body += chunk.toString());
req.on('end', () => {
const user = JSON.parse(body);
res.writeHead(201, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'User created', user }));
});
}
else {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 - Page Not Found</h1>');
}
});
Reading Request Body:
javascript
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
const data = JSON.parse(body);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ received: data }));
} catch (err) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
}
});
Handling File Uploads:
javascript
const fs = require('fs');
const server = http.createServer((req, res) => {
if (req.url === '/upload' && req.method === 'POST') {
const writeStream = fs.createWriteStream('uploaded-file.txt');
req.pipe(writeStream);
req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'File uploaded successfully' }));
});
req.on('error', (err) => {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Upload failed' }));
});
}
});
Making HTTP Requests:
javascript
// GET request
const options = {
hostname: 'jsonplaceholder.typicode.com',
port: 80,
path: '/posts/1',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
const req = http.request(options, (res) => {
console.log(`Status: ${res.statusCode}`);
console.log(`Headers: ${JSON.stringify(res.headers)}`);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response:', JSON.parse(data));
});
});
req.on('error', (err) => {
console.error('Request error:', err);
});
req.end();
// Simpler GET request with http.get()
http.get('http://jsonplaceholder.typicode.com/posts/1', (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => console.log(JSON.parse(data)));
});
HTTPS Server:
javascript
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem')
};
const httpsServer = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS World!');
});
httpsServer.listen(443, () => {
console.log('HTTPS server running on port 443');
});
6. url Module
Parses, formats, and resolves URLs.
javascript
const url = require('url');
URL Parsing (Legacy API):
javascript
// Legacy parse method (still useful)
const legacyUrl = url.parse('https://user:pass@example.com:8080/p/a/t/h?query=string#hash');
console.log(legacyUrl);
// {
// protocol: 'https:',
// slashes: true,
// auth: 'user:pass',
// host: 'example.com:8080',
// port: '8080',
// hostname: 'example.com',
// hash: '#hash',
// search: '?query=string',
// query: 'query=string',
// pathname: '/p/a/t/h',
// path: '/p/a/t/h?query=string',
// href: 'https://user:pass@example.com:8080/p/a/t/h?query=string#hash'
// }
// Parse query string
const parsedWithQuery = url.parse('/path?name=John&age=30', true);
console.log(parsedWithQuery.query); // { name: 'John', age: '30' }
WHATWG URL API (Modern):
javascript
// Modern URL class (WHATWG standard)
const myURL = new URL('https://example.com:8080/path/to/file?q=query#hash');
console.log('href:', myURL.href); // Full URL
console.log('protocol:', myURL.protocol); // https:
console.log('hostname:', myURL.hostname); // example.com
console.log('port:', myURL.port); // 8080
console.log('host:', myURL.host); // example.com:8080
console.log('origin:', myURL.origin); // https://example.com:8080
console.log('pathname:', myURL.pathname); // /path/to/file
console.log('search:', myURL.search); // ?q=query
console.log('hash:', myURL.hash); // #hash
console.log('username:', myURL.username); // '' (empty)
console.log('password:', myURL.password); // '' (empty)
// Working with search params
myURL.searchParams.append('page', '2');
console.log(myURL.href); // https://example.com:8080/path/to/file?q=query&page=2#hash
myURL.searchParams.set('q', 'newquery');
console.log(myURL.search); // ?q=newquery&page=2
console.log(myURL.searchParams.get('page')); // 2
console.log(myURL.searchParams.has('q')); // true
myURL.searchParams.delete('page');
console.log(myURL.search); // ?q=newquery
// Iterate over search params
for (const [key, value] of myURL.searchParams) {
console.log(`${key}: ${value}`);
}
// Convert to string
console.log(myURL.toString());
URL Formatting and Resolving:
javascript
// Format URL from components
const formatted = new URL('/path', 'https://example.com');
console.log(formatted.href); // https://example.com/path
// Resolve relative URLs
console.log(url.resolve('/one/two/three', 'four')); // /one/two/four
console.log(url.resolve('http://example.com/one/', 'two')); // http://example.com/one/two
console.log(url.resolve('http://example.com/one', '/two')); // http://example.com/two
// URL formatting (legacy)
const formattedLegacy = url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
query: { q: 'search' }
});
console.log(formattedLegacy); // https://example.com/some/path?q=search
Practical Examples:
javascript
// Parse request URL in HTTP server
const server = http.createServer((req, res) => {
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
console.log('Path:', parsedUrl.pathname);
console.log('Query params:', parsedUrl.searchParams.toString());
const page = parsedUrl.searchParams.get('page') || '1';
const limit = parsedUrl.searchParams.get('limit') || '10';
res.end(`Page: ${page}, Limit: ${limit}`);
});
// Validate URL
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
// Extract domain from URL
function getDomain(urlString) {
try {
const url = new URL(urlString);
return url.hostname;
} catch (err) {
return null;
}
}
// Build API URL with query parameters
function buildApiUrl(baseUrl, endpoint, params = {}) {
const url = new URL(endpoint, baseUrl);
Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, value);
});
return url.toString();
}
console.log(buildApiUrl('https://api.example.com', '/users', {
page: 1,
limit: 20,
sort: 'name'
}));
// https://api.example.com/users?page=1&limit=20&sort=name
These core modules form the foundation of Node.js development and are essential for building robust applications!
Comments (0)
No comments yet. Be the first to comment!