Exercise-4 Msd Lab Programs (1)
Exercise-4 Msd Lab Programs (1)
Description:
Express is a routing and Middleware framework for handling the different routing of the
webpage and it works between the request and response cycle. Middleware in Express.js is a
function that is executed between the incoming request and the outgoing response. The main
arguments of a middleware function are the request object, response object, and also
the next middleware function defined in the application.
Any middleware can be loaded in an Express application using the app.use() method.
The app.use() method accepts one string parameter path, which is optional, and one function
parameter callback which is the mandatory middleware function.
app.use(PATH, CALLBACK)
PROCEDURE STEPS:
Step 1: Create a midelware.js file in VSCODE.
Step 2: Creating a package.json file using following command:
PS D:\MSD LAB\ Exercise-4 >npm init
Is this OK? (yes) yes
Step 3: Install Express.js using following command:
PS D:\Project> npm i express -g
Step 4: To use middleware in the Express application, we need to install it.
PS D:\MSD LAB\ Exercise-4 >npm install morgan
Step5: Install path using following command:
//This is an exact copy of the NodeJS ’path’ module published to the NPM registry.
PS D:\MSD LAB\ Exercise-4 >npm i path
Step 6: Install morgan-body using following command:
PS D:\MSD LAB\ Exercise-4 >npm i morgan-body
Step 7: Start the server using the following node command.
PS D:\MSD LAB\ Exercise-4 >node midelware.js press enter button then server starts.
Step 8: Open the browser and enter URL as http://localhost:5000 then output will be
displayed on the webpage.
OUTPUT: 4(a)
Now, if you want to see the postRequest output, Open the postman tool and select the
post method and enter the URL http://localhost:5000/postRequest and click on the send
button.
Then, select the Body and click on preview then output will be displayed.
:
3. Click on Windows 64 – bit button then downloaded as shown in below webpage.
4. Select the executable file in downloads in your system and right click on it and click on Run
as administrator then postman s/w is installed in your PC and postman icon placed on
desktop.
postRequest output 2:
Exercise-4(b): Express.js -Mongoose, MongoDB
AIM: Write a Express.js program to write a Mongoose schema to connect with MongoDB
Description:
Mongo DB is a document-oriented database. It is an open source product, developed and
supported by a company named 10gen. MongoDB is a document database. It stores data
in a type of JSON format called BSON.
MongoDB is a scalable, open source, high performance, document-oriented database.
MongoDB is the most popular NoSQL database. The term ‘NoSQL’ means ‘non-
relational’. It means that MongoDB isn’t based on the table-like relational database
structure but provides an altogether different mechanism for storage and retrieval of data.
This format of storage is called BSON ( similar to JSON format).
Mongoose is an Object Data Modeling (ODM) tool, which is created to work on an
asynchronous MongoDB environment. It imposes the structure and constraints by
mapping JavaScript objects to documents in the database.
It also provides the functionality for validating field values and methods for selecting,
adding, modifying, and deleting documents in the collection.
Step 3: Select mongoDB software and right click on it and click on Install.
Once download is complete open the msi file. Click Next in the start up screen.
Step 4: Accept the End-User License Agreement and click on the next button.
Step 7: Select the MongoDB compas and Click on the Install button to start the installation.
Select the Path from System variables and click on Edit button
PROCEDURE STEPS:
Step1: Installing two packages express and mongoose for mongodb database:
For installing the Mongoose library, issue the below command in the Node command
prompt.
PS D:\MSD LAB\ Exercise-4 > npm i express mongoose
Step2:Connecting to MongoDB using Mongoose(open mongoDB database):
To establish a connection to the MongoDB database, create a connection object using
Mongoose. This is done through the below code:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/Database_Name');
Step3: Create a notes.js file in VSCODE or Node.js command prompt .
Step4: Start the server using the following node command.
PS D:\MSD LAB\ Exercise-4 > node notes.js press enter button then server starts.
Step 5: Open the postman and select the POST from the list box and enter the URL
http://localhost:5000/users
Step 6: Open the Body, click on the none list box and select the raw and select JSON from
second list box.
Step 7: Type the following code
{
“name”: “Karth”,
“email”: “[email protected]”
}
Step 8: Click on the send button and click on Preview then output will be displayed
in JSON format.
Step 9: Now, open the MongoDB database, see your database, collection and document.
(The output data is transferred from server to client and also to MongoDB database).
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/Meanstack', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
process.exit(1); // Exit the application if unable to connect to MongoDB
});
// Create a new user document with name and email from request body
const newUser = new User({
name: req.body.name,
email: req.body.email
});