Standard Library of JavaScript (Node.js): A Complete Overview
Node.js is a JavaScript runtime environment built on the V8 engine. One of its main advantages is the rich standard library, which provides hundreds of ready-made modules for solving everyday backend development tasks: from working with the file system to creating HTTP servers. In this article, we will look at the key modules, their capabilities, and provide practical code examples.
1. Module fs (File System)
The fs module is the foundation for working with files and directories. It supports both synchronous and asynchronous operations.
- Reading a file (asynchronous):
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data);});- Writing to a file:
fs.writeFile('output.txt', 'Hello, Node.js!', (err) => { if (err) throw err; console.log('File saved');});- Working with directories (creating, deleting, reading):
// Creating a folderfs.mkdir('newDir', { recursive: true }, (err) => { if (err) throw err; console.log('Folder created');});
// Reading the contents of a folderfs.readdir('.', (err, files) => { console.log(files); // array of file names});2. Module path
The path module simplifies working with file and folder paths, regardless of the operating system.
- Joining paths:
const path = require('path');
const fullPath = path.join('users', 'admin', 'docs', 'file.txt');console.log(fullPath); // users/admin/docs/file.txt (on Linux) or users\\admin\\docs\\file.txt (on Windows)- Getting the file extension:
const ext = path.extname('image.png');console.log(ext); // '.png'- Normalizing a path (removes extra ".." and "."):
const normalized = path.normalize('/foo/bar//baz/asdf/quux/..');console.log(normalized); // /foo/bar/baz/asdf3. Module http
Allows you to create HTTP servers and clients. Frameworks like Express.js are built on this module.
- Simple HTTP server:
const http = require('http');
const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, world!');});
server.listen(3000, () => { console.log('Server is running on port 3000');});- HTTP client (making a GET request):
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)));});4. Module events
Implements the "Observer" pattern (EventEmitter). Allows you to create custom events and subscribe to them.
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
// Subscribing to an eventmyEmitter.on('greet', (name) => { console.log(`Hello, ${name}!`);});
// Emitting an eventmyEmitter.emit('greet', 'Alexey'); // Hello, Alexey!5. Module buffer
Buffer is a temporary storage for binary data. It is used when working with streams, files, and network protocols.
// Creating a buffer from a stringconst buf = Buffer.from('Hello', 'utf8');console.log(buf); // <Buffer 48 65 6c 6c 6f>
// Concatenating buffersconst buf1 = Buffer.from('Node');const buf2