Skip to content

Latest commit

 

History

History
73 lines (57 loc) · 2.61 KB

core_modules.md

File metadata and controls

73 lines (57 loc) · 2.61 KB

Core Modules in Node.js

Overview

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.

1. HTTP Module

The HTTP module is one of the key modules for creating web servers and handling HTTP requests and responses.

Creating a Simple Web Server

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.

2. File System Module (fs)

The File System module allows you to work with the file system on your computer.

Reading a File Asynchronously

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.

3. Path Module

The Path module provides utilities for working with file and directory paths.

Example: Formatting 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.

4. Events Module

Node.js is built around an event-driven architecture, primarily using the EventEmitter class, which is part of the Events module.

Example: Emitting and Handling Events

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.

Conclusion

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.