Node.js comes with a rich library of various core modules which are compiled into the binary distribution and load automatically when Node.js processes start. These modules provide foundational functionalities necessary for building scalable applications.
The HTTP module is one of the key modules for creating web servers and handling HTTP requests and responses.
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
This example demonstrates how to create a basic web server that listens on port 3000.
The File System module allows you to work with the file system on your computer.
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
This function reads the contents of example.txt
asynchronously, without blocking other operations.
The Path module provides utilities for working with file and directory paths.
const path = require('path');
const filePath = path.join('/users/joe', 'test.txt');
console.log(filePath);
This code snippet demonstrates how to use the path.join
method to create a consistent file path across different operating systems.
Node.js is built around an event-driven architecture, primarily using the EventEmitter class, which is part of the Events module.
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
This example shows how to create an event emitter instance, listen for an "event", and trigger the event.
These core modules form the backbone of many Node.js applications, providing essential functionalities that are needed for developing robust server-side logic. Understanding and utilizing these modules effectively can greatly enhance your application's performance and scalability.
For more detailed information and additional functionalities of each core module, refer to the official Node.js API documentation.