Back to Web Development
2026-04-055 min read

Core Modules (Web Development)

Learn Core Modules (Web Development) step by step with clear examples and exercises.

Title: Core Modules (Web Development) - A full guide

Why This Matters

Understanding core modules is crucial for web development as it allows you to expand your website's functionality by utilizing built-in JavaScript libraries in Node.js. This knowledge will help you tackle real-world programming challenges, impress interviewers, and debug common issues that arise during the development process.

Core modules provide a foundation for building robust and efficient applications, making them an essential part of any web developer's toolkit.

Prerequisites

Before diving into core modules, it is essential to have a good understanding of:

  1. HTML (Hypertext Markup Language)
  2. CSS (Cascading Style Sheets)
  3. JavaScript basics
  4. Familiarity with the Node.js environment
  • Understanding how to install and run Node.js applications
  • Basic knowledge of the Node.js REPL (Read-Eval-Print Loop)

Core Concept

Core modules are built-in libraries in Node.js that provide various functionalities without requiring external dependencies. To use a core module, simply import it at the beginning of your script using the require() function.

const { ModuleName } = require('module-name');

Replace ModuleName with the name of the desired core module. Here are some commonly used core modules:

  1. fs (File System): This module allows you to read, write, and manipulate files on your system. You can use functions like readFile(), writeFile(), and rename() to interact with files.
const fs = require('fs');
fs.readFile('./data.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
  1. http: Use this module for creating HTTP servers and clients in Node.js. You can create a simple HTTP server using the createServer() function.
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, () => {
console.log('Server running at http://localhost:3000/');
});
  1. path: The path module provides utilities for working with file and directory paths. You can use functions like join() to combine multiple paths.
const path = require('path');
console.log(path.join('/user', 'profile', 'info.txt')); // Output: /user/profile/info.txt
  1. crypto: This module includes cryptographic functionality such as encryption, decryption, hashing, and random number generation. You can use functions like createHash() for hashing data.
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex')); // Output: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
  1. os: The os module provides information about the operating system on which Node.js is running. You can use functions like hostname() to get the hostname of the machine.
const os = require('os');
console.log(os.hostname()); // Output: Your-Computer-Name
  1. events: Use this module to create event-driven programs in Node.js. You can use the EventEmitter class to emit and listen for events.
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.on('message', (data) => {
console.log(data);
});

emitter.emit('message', 'Hello World');

Worked Example

Let's create a simple HTTP server using the http core module and serve an HTML file:

  1. Create a folder named my-app. Inside this folder, create two files: server.js and index.html.
  1. Add the following content to index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
</head>
<body>
<h1>Welcome to My App</h1>
</body>
</html>
  1. Add the following content to server.js:
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile('./index.html', 'utf8', (err, data) => {
if (err) throw err;
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404 Not Found');
}
});

server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
  1. Save both files and run the server.js script using Node.js in your terminal:
node server.js
  1. Open your browser and visit http://localhost:3000/. You should see the "Welcome to My App" message.

Common Mistakes

  1. Forgetting to require() the module: Always ensure that you import the required core module at the beginning of your script using the require() function.
  1. Incorrect module name: Verify that the specified module name is correct and spelled correctly.
  1. Not defining a callback function for events: When working with event-driven programs, make sure to define a callback function for handling events.
  1. Misusing file system functions: Be mindful of how you use file system functions such as readFile(), writeFile(), and rename(). Make sure to handle errors appropriately.
  1. Not closing connections: In HTTP servers, it is essential to close the connection after sending a response to prevent resource exhaustion.

Additional Common Mistakes

  1. Using core modules in client-side JavaScript (in a browser): Core modules are exclusively for server-side development using Node.js. For client-side JavaScript, you can use libraries such as jQuery or vanilla JavaScript.
  1. Not checking for the existence of files before reading them: Always check if a file exists before trying to read it to avoid errors.
  1. Not handling errors properly: Make sure to handle all potential errors that may occur during the execution of your script, such as file not found or permission denied errors.

Practice Questions

  1. Write a script that uses the fs module to read the contents of a file named data.txt.
const fs = require('fs');
fs.readFile('./data.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
  1. Create an HTTP server using the http module that serves a static HTML file named index.html.

See the worked example above for a simple implementation.

  1. Implement a simple encryption function using the crypto module.
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex')); // Output: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
  1. Write a script that listens for incoming connections on port 8080 and logs the client's IP address and requested resource.
const http = require('http');

const server = http.createServer((req, res) => {
console.log(`Client ${req.socket.remoteAddress} requested ${req.url}`);
});

server.listen(8080, () => {
console.log('Server running at http://localhost:8080/');
});

FAQ

  1. What is the difference between core modules and third-party modules in Node.js?

Core modules are built-in libraries provided by Node.js, while third-party modules are external packages created by developers and published on npm (Node Package Manager).

  1. How can I find a list of all available core modules in Node.js?

You can view the complete list of core modules by visiting the official Node.js documentation or running npm help in your terminal.

  1. Can I use core modules with client-side JavaScript (in a browser)?

No, core modules are exclusively for server-side development using Node.js. For client-side JavaScript, you can use libraries such as jQuery or vanilla JavaScript.

  1. Why should I use core modules instead of third-party packages?

Core modules offer built-in functionality and are optimized for performance, while third-party packages may have varying quality and performance levels. Using core modules ensures consistency and compatibility across different Node.js applications.

Core Modules (Web Development) | Web Development | XQA Learn