0% found this document useful (0 votes)
4 views9 pages

Nodejs Application With Mongodb

The document outlines the procedure for creating a back-end web application using NodeJS and MongoDB for registering Indian dogs. It includes steps for setting up the server, connecting to the database, and handling form submissions. The application successfully registers dog information and stores it in a MongoDB database, confirmed through MongoDB Compass.

Uploaded by

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

Nodejs Application With Mongodb

The document outlines the procedure for creating a back-end web application using NodeJS and MongoDB for registering Indian dogs. It includes steps for setting up the server, connecting to the database, and handling form submissions. The application successfully registers dog information and stores it in a MongoDB database, confirmed through MongoDB Compass.

Uploaded by

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

AKASH S (71812211005)

EX.NO: 09
NODEJS APPLICATION WITH MONGODB
DATE:

AIM:
To design a back-end web application using NodeJS with MongoDB for Indian Dog
registrations.

PROCEDURE:

1. Open cmd and navigate to it using ‘cd’


Install the necessary dependencies for the Node server:
npm init -y
npm install express body-parser mongodb
2. Use the require function to use the packages like express and mongodb
3. Connect to the database using the connection string:
client = new MongoClient(url).connect()
4. Insert the documents into the database using
client.db(“DBName”).collection(“CollectionName”).insertOne({data})
5. Run the server using:
node ex9_server.js

CODE

FSD Ex 8.html

<html>
<head>
<title>Indian Dog Breeds Registration</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
AKASH S (71812211005)

justify-content: center;
align-items: center;
height: 100vh;
}
.form-container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
h1 {
color: #8B4513;

font-size: 24px;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
input,
select {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 14px;
}
button {
background-color: #8B4513;
color: white;
border: none;
AKASH S (71812211005)

padding: 10px 20px;


border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #A0522D;
}
.reset-button {
background-color: #ccc;
color: #333;
margin-left: 10px;
}

.reset-button:hover {
background-color: #bbb;
}
</style>
</head>

<body>

<!-- Dog Registration Form -->


<div class="form-container">
<h1>Indian Dog Breeds Registration</h1>
<form name="dogForm" action="http://localhost:3000/register-dog" method="POST">
<!-- Dog Name -->
<label for="dogName">Dog Name</label>
<input type="text" id="dogName" name="dogName" placeholder="Enter Dog Name"
required>

<!-- Breed Selection -->


<label for="breed">Breed</label>
<select id="breed" name="breed" required>
<option value="" disabled selected>Select Breed</option>
<option value="Rajapalayam">Rajapalayam</option>
<option value="Mudhol Hound">Mudhol Hound</option>
AKASH S (71812211005)

<option value="Indian Pariah">Indian Pariah</option>


<option value="Kanni">Kanni</option>
<option value="Chippiparai">Chippiparai</option>
<option value="Combai">Combai</option>
<option value="Rampur Hound">Rampur Hound</option>
<option value="Pandikona">Pandikona</option>
</select>

<!-- Age -->


<label for="age">Age</label>
<input type="number" id="age" name="age" placeholder="Enter Age (in years)" required>

<!-- Owner's Name -->


<label for="ownerName">Owner's Name</label>
<input type="text" id="ownerName" name="ownerName" placeholder="Enter Owner's Name"
required>

<!-- Contact Information -->


<label for="contact">Contact Number</label>
<input type="tel" id="contact" name="contact" placeholder="Enter Contact Number" required>

<!-- Submit and Reset Buttons -->


<div>
<button type="submit">Register Dog</button>
<button type="reset" class="reset-button">Reset</button>
</div>
</form>
</div>
</body>
</html>
AKASH S (71812211005)

Ex9_server.js

const express = require('express');


const bodyParser = require('body-parser');
const { MongoClient } = require('mongodb');

const app = express();


const port = 8000;

// MongoDB Connection URL


const url = "mongodb+srv://am_akash1309:[email protected]/";
const client = new MongoClient(url);

app.use(bodyParser.urlencoded({ extended: false }));


app.use(bodyParser.json());

async function connectDB() {


try {
await client.connect();

console.log("Connected to MongoDB Atlas");


} catch (error) {
console.error("MongoDB Connection Error:", error);
}
}
connectDB();

app.post('/register-dog', async (req, res) => {


const { dogName, breed, age, ownerName, contact } = req.body;

try {
const db = client.db("Indian-Dog-Registration");
const collection = db.collection("Owners");
const result = await collection.insertOne({
dogName,
breed,
age: Number(age),
AKASH S (71812211005)

ownerName,
contact
});

console.log("Dog Registered:", result.insertedId);


res.send(`Thank you, ${ownerName}! Your ${breed} dog, ${dogName}, has been registered
successfully.`);
} catch (error) {
console.error("Error registering dog:", error);
res.status(500).send("Failed to register the dog.");
}
});

// Start the server


app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
AKASH S (71812211005)

OUTPUT
AKASH S (71812211005)
AKASH S (71812211005)

INFERENCE
The form once submitted will send a POST request to the backend server. The sent data will be
received in the backend server and will process it and save in in the database. This can be confirmed in
MongoDB Compass.

RESULT:
Thus, back-end web application using NodeJS with MongoDB for Indian Dog registrations has
been verified and executed successfully.

You might also like