app-generator / docs

App Generator - The Official Documentation | AppSeed
https://docs.appseed.us
1 stars 1 forks source link

[Node] Modules #116

Open mahfujul-helios opened 1 month ago

mahfujul-helios commented 1 month ago

Modules

What is a Module in Node.js?

In Node.js, a module is essentially a reusable block of code that encapsulates related functionality. It allows you to organize your code into separate files and logically group related functions, variables, and classes together. Modules help in keeping your codebase organized, modular, and easy to maintain.

Node.js Built-in Modules

Node.js comes with a set of built-in modules that provide various functionalities to developers without the need for additional installations. Here's a table listing these modules along with their brief descriptions:

Module Description
assert Provides a set of assertion tests.
buffer Used to handle binary data.
child_process Allows running a child process.
cluster Enables splitting a single Node process into multiple processes.
crypto Handles OpenSSL cryptographic functions.
dgram Provides an implementation of UDP datagram sockets.
dns Performs DNS lookups and name resolution functions.
domain Deprecated. Previously used to handle unhandled errors.
events Facilitates handling events.
fs Handles the file system operations.
http Enables Node.js to act as an HTTP server.
https Enables Node.js to act as an HTTPS server.
net Allows creating servers and clients for network communication.
os Provides information about the operating system.
path Handles file paths.
punycode Deprecated. Previously used for a character encoding scheme.
querystring Handles URL query strings.
readline Facilitates handling readable streams one line at a time.
stream Handles streaming data.
string_decoder Decodes buffer objects into strings.
timers Executes a function after a given number of milliseconds.
tls Implements TLS and SSL protocols.
tty Provides classes used by a text terminal.
url Parses URL strings.
util Accesses utility functions.
v8 Accesses information about V8 (the JavaScript engine).
vm Compiles JavaScript code in a virtual machine.
zlib Compresses or decompresses files.

Create Your Own Modules

Create a file std.js

const getname = ()=>{
    return "badon"
}

 const getAge = ()=>{
    return "23"
}

const roll = 25012
const cgpa = 3.33

module.exports = {
    getname,
    getAge,
    roll,
    cgpa
}

Then create a file index.js to import those.

const s1 = require('./std')
console.log(s1.getname())
console.log(s1.getAge())
console.log(s1.cgpa)
console.log(s1.roll)

Output Now run the command

node index.js 

badon
23
3.33
25012

Some built in example

// Example using Node.js built-in modules

// Importing the 'fs' (File System) module
const fs = require('fs');

// Reading a file asynchronously using fs.readFile
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  console.log('File contents:', data);
});

// Importing the 'http' module
const http = require('http');

// Creating an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World!\n');
});

// Listening on port 3000
server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

conclusion

Node.js modules are an integral part of building scalable, efficient, and maintainable applications in the Node.js runtime environment. These modules encapsulate related functionality, allowing developers to organize their code into reusable components, enhancing code readability, and facilitating collaboration within development teams.

Node.js offers two main types of modules: built-in modules and custom modules. Built-in modules, provided by Node.js itself, offer functionalities such as file system operations, networking, cryptography, and more, making them readily available for use without additional installations. Custom modules, on the other hand, are created by developers to encapsulate specific functionalities within their applications and can be shared across different parts of the application or even between different applications.

By leveraging modules, developers can effectively manage dependencies, improve code reusability, and facilitate code maintenance. Whether it's handling file operations, serving HTTP requests, or implementing complex cryptographic functions, Node.js modules provide a robust foundation for building a wide range of applications, from simple scripts to large-scale web applications and microservices architectures. As such, understanding how to effectively use and create Node.js modules is essential for mastering Node.js development and building high-quality, scalable applications.