Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

Pract 1

Node.js practical 1 MCA

Uploaded by

ssghadge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Pract 1

Node.js practical 1 MCA

Uploaded by

ssghadge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRACTICAL NO 1

Aim: Create an application to demonstrate Node.js Modules


Objective:

Create an app that:

 Uses a core module (fs, path, os)


 Uses a local (custom) module (mathOperations.js)
 Uses a third-party module (lodash, installed via npm)

Theory:
 What is Node.js?

Node.js is a free, open-source JavaScript runtime that runs on Windows, Mac, Linux, and more.

It lets you execute JavaScript code outside of a web browser, enabling server-side development
with JavaScript.

Built on Chrome's V8 JavaScript engine, Node.js is designed for building scalable network
applications efficiently.

 Why Node.js?

Node.js excels at handling many simultaneous connections with minimal overhead, making it
perfect for:

 Real-time applications (chats, gaming, collaboration tools)


 APIs and microservices
 Data streaming applications
 Command-line tools
 Server-side web applications

Its non-blocking, event-driven architecture makes it highly efficient for I/O-heavy workloads.

 What Can Node.js Do?

 Web Servers: Create fast, scalable network applications


 File Operations: Read, write, and manage files on the server
 Database Interaction: Work with databases like MongoDB, MySQL, and more
 APIs: Build RESTful services and GraphQL APIs
 Real-time: Handle WebSockets for live applications
 CLI Tools: Create command-line applications

 What is a Node.js File?

Node.js files contain code that runs on the server. They usually have the .js extension and can
be started with the node command.
 Node.js files run tasks when certain events happen (like a web request)
 They must be started on the server to have any effect
 They use JavaScript syntax

 Example: Running a Node.js File


node app.js

 Download and Install Node.js

1. Go to https://nodejs.org
2. Download the LTS (Long Term Support) version
3. Run the installer and follow the instructions

 What is a Module in Node.js?

Modules are the building blocks of Node.js applications, allowing you to organize code into
logical, reusable components. They help in:

 Organizing code into manageable files


 Encapsulating functionality
 Preventing global namespace pollution
 Improving code maintainability and reusability

There are three main types of modules in Node.js:

🔹 1. Built-in (Core) Modules

These are modules provided by Node.js itself—no installation needed.

Module Description
fs File system operations (read, write, delete files)
http Create HTTP server and handle requests
path Work with file and directory paths
url Parse and manipulate URLs
os Info about the operating system
events Handle events and listeners
crypto Perform encryption, decryption, and hashing
buffer Work with binary data
stream Handle streaming data (e.g., reading a big file)
🔹 2. Local (Custom) Modules

These are user-defined modules written in your own .js files.

Example:
A file like mathOperations.js exporting functions:

module.exports = {
add: (a, b) => a + b,
};
Used in another file:
const math = require('./mathOperations');

console.log(math.add(2, 3));

🔹 3. Third-party Modules

These are modules installed from npm (Node Package Manager).

You need to install them first:

npm install express

Then use in your code:

CopyEdit
const express = require('express');
const app = express();

Popular third-party modules:

Module Purpose
express Web framework
mongoose MongoDB integration
lodash Utility functions
axios HTTP client
dotenv Manage environment variables

📁 Project Structure
node-modules-demo/
├── app.js ← Main file
├── mathOperations.js ← Local module
├── package.json ← NPM metadata file

Steps to Run a Node.js Program in VS Code


Step 1: Create a Project Folder

1. Open VS Code.
2. Click on File → Open Folder and create a new folder, e.g., NodeModuleDemo.
Step 2: Initialize the Project

1. Open the terminal in VS Code (Ctrl + ~)


2. Run: npm init –y

This creates a package.json file.

Step 3: Create Your Module Files

1. mathOperations.js (Custom Module)

Create this file inside your folder:

// mathOperations.js
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
// Exporting functions as module
module.exports = {
add, multiply
};
2. app.js (Main File)
// app.js
const math = require('./mathOperations');
const result1 = math.add(10, 5);
const result2 = math.multiply(10, 5);
console.log(`Addition: ${result1}`);
console.log(`Multiplication: ${result2}`);

Step 4: Run the Node.js Program


In the VS Code terminal, run:

node app.js

Output:

Addition: 15

Multiplication: 50

You might also like