Node.js APIs: A Comprehensive Guide for Backend Development
Node.js provides powerful built-in APIs that shape the backbone of backend development. I’ll share my experience with these essential APIs and demonstrate their practical applications.
File System (fs) API
The fs module stands as a critical component for file operations. In my projects, I frequently use it for data persistence and configuration management. Here’s a practical example of file operations:
const fs = require('fs').promises;
async function handleFiles() {
try {
// Read file
const data = await fs.readFile('input.txt', 'utf8');
// Process data
const processed = data.toUpperCase();
// Write to new file
await fs.writeFile('output.txt', processed);
// Read directory contents
const files = await fs.readdir('./');
console.log('Directory contents:', files);
} catch (error) {
console.error('File operation failed:', error);
}
}
HTTP/HTTPS API
Creating web servers and handling network requests form the foundation of web applications. Here’s how I implement a basic HTTPS server:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem')
};
const server = https.createServer(options, (req, res) => {
if (req.url === '/api/data') {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({message: 'Success'}));
} else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(3000, () => console.log('Server running on port 3000'));
Path API
The Path module ensures consistent file path handling across different operating systems. I use it extensively for maintaining platform independence:
const path = require('path');
function configureFilePaths() {
const basePath = process.cwd();
const configPath = path.join(basePath, 'config', 'settings.json');
const relativePath = path.relative(basePath, configPath);
console.log('Base Path:', basePath);
console.log('Config Path:', configPath);
console.log('Relative Path:', relativePath);
return {
extension: path.extname(configPath),
fileName: path.basename(configPath),
dirName: path.dirname(configPath)
};
}
Events API
The Events module implements the Observer pattern through EventEmitter. Here’s how I create custom event-driven systems:
const EventEmitter = require('events');
class TaskManager extends EventEmitter {
constructor() {
super();
this.tasks = [];
}
addTask(task) {
this.tasks.push(task);
this.emit('taskAdded', task);
}
completeTask(taskId) {
const task = this.tasks.find(t => t.id === taskId);
if (task) {
task.completed = true;
this.emit('taskCompleted', task);
}
}
}
const taskManager = new TaskManager();
taskManager.on('taskAdded', (task) => {
console.log('New task added:', task);
});
taskManager.on('taskCompleted', (task) => {
console.log('Task completed:', task);
});
Stream API
Streams enable efficient handling of large data sets and real-time information. Here’s my approach to implementing streaming:
const fs = require('fs');
const zlib = require('zlib');
function processLargeFile() {
const readStream = fs.createReadStream('large-file.txt');
const writeStream = fs.createWriteStream('processed-file.txt.gz');
const gzip = zlib.createGzip();
readStream
.pipe(gzip)
.pipe(writeStream);
readStream.on('error', (error) => console.error('Read error:', error));
writeStream.on('error', (error) => console.error('Write error:', error));
writeStream.on('finish', () => console.log('Processing completed'));
}
Crypto API
Security remains paramount in modern applications. The Crypto module provides essential security operations:
const crypto = require('crypto');
class SecurityManager {
constructor() {
this.algorithm = 'aes-256-cbc';
this.key = crypto.randomBytes(32);
this.iv = crypto.randomBytes(16);
}
encrypt(text) {
const cipher = crypto.createCipheriv(this.algorithm, this.key, this.iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
decrypt(encrypted) {
const decipher = crypto.createDecipheriv(this.algorithm, this.key, this.iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
generateHash(data) {
return crypto
.createHash('sha256')
.update(data)
.digest('hex');
}
}
Integrating Multiple APIs
Real-world applications often require combining multiple APIs. Here’s a comprehensive example that demonstrates this integration:
const fs = require('fs').promises;
const path = require('path');
const https = require('https');
const crypto = require('crypto');
const EventEmitter = require('events');
class DataProcessor extends EventEmitter {
constructor(inputDir, outputDir) {
super();
this.inputDir = inputDir;
this.outputDir = outputDir;
}
async processFiles() {
try {
// Read directory
const files = await fs.readdir(this.inputDir);
for (const file of files) {
const inputPath = path.join(this.inputDir, file);
const outputPath = path.join(this.outputDir, `processed_${file}`);
// Read and hash content
const content = await fs.readFile(inputPath, 'utf8');
const hash = crypto
.createHash('sha256')
.update(content)
.digest('hex');
// Process and save
await fs.writeFile(outputPath, content.toUpperCase());
this.emit('fileProcessed', {
file,
hash,
timestamp: new Date()
});
}
} catch (error) {
this.emit('error', error);
}
}
async fetchAndProcess(url) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', async () => {
try {
const outputPath = path.join(this.outputDir, 'api_data.json');
await fs.writeFile(outputPath, data);
resolve(data);
} catch (error) {
reject(error);
}
});
}).on('error', reject);
});
}
}
// Usage example
async function main() {
const processor = new DataProcessor('./input', './output');
processor.on('fileProcessed', (info) => {
console.log('Processed file:', info);
});
processor.on('error', (error) => {
console.error('Processing error:', error);
});
await processor.processFiles();
await processor.fetchAndProcess('https://api.example.com/data');
}
These Node.js APIs create robust, efficient, and secure backend applications. The combination of these APIs enables handling various aspects of server-side development, from file operations to security implementations. Understanding and effectively using these APIs remains essential for building scalable applications.
The examples provided demonstrate practical implementations while maintaining best practices in error handling, asynchronous operations, and event-driven programming. They serve as building blocks for more complex applications.