0% found this document useful (0 votes)
12 views19 pages

Experiment 14,15,16, 17 Out Put Process

The document outlines experiments involving HTTP and File System modules in Node.js, detailing server creation and file handling. It also covers Buffers, Streams, and EventEmitter for managing binary data and asynchronous events. Additionally, it provides steps to install Express, implement the MVC pattern, and structure an Express.js application with MongoDB integration.

Uploaded by

payakulatarun
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)
12 views19 pages

Experiment 14,15,16, 17 Out Put Process

The document outlines experiments involving HTTP and File System modules in Node.js, detailing server creation and file handling. It also covers Buffers, Streams, and EventEmitter for managing binary data and asynchronous events. Additionally, it provides steps to install Express, implement the MVC pattern, and structure an Express.js application with MongoDB integration.

Uploaded by

payakulatarun
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/ 19

Experiment 14: Http and File System:

1. HTTP Module (Creating a Simple Server)


The http module allows you to create an HTTP server and handle requests.

Basic Server Creation


OUTPUT:

2. File System Module (fs)


The fs module allows you to work with the file system, including reading, writing, and deleting
files.

OUTPUT:
Experiment 15:

1. Buffers (Handling Binary Data)


Buffers are used to handle raw binary data directly in memory, making them useful for working
with files, network sockets, and streams.
Buffers store binary data and can be converted to a string using .toString().

2. Streams (Handling Large Data Efficiently)


Streams allow you to process data in chunks instead of loading everything into memory, making
them ideal for handling large files or network requests.

Types of Streams

 Readable (e.g., reading a file)


 Writable (e.g., writing to a file)
 Duplex (both readable & writable, e.g., sockets)
 Transform (modifies data, e.g., compression)
Example: Reading a File Using Streams
Output:

3. Events (Event-Driven Programming)


Node.js uses the EventEmitter module to handle custom events asynchronously.
Example: Creating an EventEmitter

Experiment:16: Steps to Install Express in a Node Project:


Step 1: Open the terminal or command prompt on your computer, and navigate to the root
directory of your project.
cd path-to-your-porject

Step 2: Initialize a NodeJS project using the following command.

npm init –y

Step 3: Install the express package using the following command.


npm install express
Project Structure:
OUTPUT:
Experiment:17

Models ,Views, and Routes

Express follows the MVC (Model-View-Controller) pattern to organize code efficiently. This guide will
walk you through how to implement Models, Views, and Routes in an Express.js application using
MongoDB (Mongoose).

 express → Web framework


 mongoose → MongoDB ORM
 ejs → Templating engine for views
 dotenv → Environment variables
 body-parser → Parses request bodies
 cors → Enables cross-origin requests

express-mvc-app/

│── models/ # Database models

│ ├── User.js

│── controllers/ # Business logic

│ ├── userController.js

│── routes/ # Routing

│ ├── userRoutes.js

│── views/ # Frontend templates (EJS)

│ ├── index.ejs

│── config/ # Database configuration

│ ├── db.js

│── server.js # Main Express server

│── .env # Environment variables

│── package.json

Step:1

Open VSCODE--- create a new folder KK----select the folder myapp ---- In myapp folder create another
folder named as models.
Inside models folder create a file named as User.js

Step2:

Inside models folder create another folder named as views----- In views folder create a two files

1. newUser.ejs

2. users.ejs

Step 3:

Create another folder in myapp i.e., routes -- inside routes folder create a file userRoutes.js

Step 4:

Inside myapp folder create a file i.e., .env

Step 5:

In myapp folder create another file named as app.js


Create a .env file in the root directory:

Create app.js
Output:

You might also like