0% found this document useful (0 votes)
52 views14 pages

Advances in Web Technologies: Lab Assignment-5

This document discusses creating databases and performing CRUD operations using Express.js and MongoDB. It includes: 1) Creating a patient and employee database with necessary fields and performing CRUD operations. 2) Creating an Express form to receive employee data and update it in the database. The code includes models, routes, and front-end form handling. 3) Creating a Node.js program to illustrate GET and POST requests using a registration form. The POST request saves form data to a JSON object and the GET request retrieves query parameters.

Uploaded by

Sangeeth Kumar
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)
52 views14 pages

Advances in Web Technologies: Lab Assignment-5

This document discusses creating databases and performing CRUD operations using Express.js and MongoDB. It includes: 1) Creating a patient and employee database with necessary fields and performing CRUD operations. 2) Creating an Express form to receive employee data and update it in the database. The code includes models, routes, and front-end form handling. 3) Creating a Node.js program to illustrate GET and POST requests using a registration form. The POST request saves form data to a JSON object and the GET request retrieves query parameters.

Uploaded by

Sangeeth Kumar
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/ 14

Advances in Web Technologies

LAB ASSIGNMENT-5
REGNO:19MID0130 NAME:SANGEETH KUMAR H

1) Create patient DB with necessary fields and Perform CURD operation.

CREATE:

READ:

UPDATE:

DELETE:
2) Create EMP DB with necessary fields and perform CURD operation.

INSERT

READ:

UPDATE:

Delete:
3) Create a Express js form to receive data ( emp data) and update in to db.

CODE:

ModelSchema-user.js:

const mongoose = require('mongoose')

const UserSchema = new mongoose.Schema(


{
emp_id: { type: String, required: true, unique: true },
dept: { type: String, required: true},
ename: { type: String, required: true},
age: { type: Number, required: true},
mail: { type: String, required: true},
mobile: { type: String, required: true},
salary: { type: Number, required: true},
doj: { type: Date, required: true}

},
{ collection: 'Employe' }
)

const model = mongoose.model('UserSchema', UserSchema)

module.exports = model

Index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Updation</h1>
<form id="update">
<input type="text" autocomplete="off" id="emp_id" placeholder="Emp
Id" />
<input type="text" autocomplete="off" id="dept"
placeholder="Department" />
<input type="text" autocomplete="off" id="ename" placeholder="Emp
Name" />
<input type="number" autocomplete="off" id="age" placeholder="Age" />
<input type="text" autocomplete="off" id="mail" placeholder="Mail" />
<input type="text" autocomplete="off" id="mobile" placeholder="Mobile
Number" />
<input type="number" autocomplete="off" id="salary"
placeholder="Salary" />
<input type="date" autocomplete="off" id="doj" placeholder="Year of
Join" />
<input type="submit" value="Submit Form" />
</form>

<script>
const form = document.getElementById('update')
form.addEventListener('submit', registerUser)

async function registerUser(event) {


event.preventDefault()
const emp_id = document.getElementById('emp_id').value
const dept = document.getElementById('dept').value
const ename = document.getElementById('ename').value
const age = document.getElementById('age').value
const mail = document.getElementById('mail').value
const mobile = document.getElementById('mobile').value
const salary = document.getElementById('salary').value
const doj = document.getElementById('doj').value
const result = await fetch('/api/updatedb', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
emp_id,
dept,
ename,
age,
mail,
mobile,
salary,
doj
})
}).then((res) => res.json())

if (result.status === 'ok') {


// everythign went fine
alert('Success')
} else {
alert(result.error)
}
}
</script>
</body>
</html>

Server.js:

const express = require('express')


const path = require('path')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const emp = require('./model/user')
mongoose.connect('mongodb://localhost:27017/emp', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})

const app = express()


app.use('/', express.static(path.join(__dirname, 'static')))
app.use(bodyParser.json())
app.post('/api/updatedb', async (req, res) => {
const {emp_id,
dept,
ename,
age,
mail,
mobile,
salary,
doj } = req.body

try {
const response = await emp.create({
emp_id,
dept,
ename,
age,
mail,
mobile,
salary,
doj
})
console.log('Updated successfully: ', response)
} catch (error) {
if (error.code === 11000) {
// duplicate key
return res.json({ status: 'error', error: 'empid not unique' })
}
throw error
}

res.json({ status: 'ok' })


})

app.listen(9999, () => {
console.log('Server up at 9999')
})

OUTPUT:
3) Create a node js program to illustrate get and Post method request
CODE:
POST:

Pat.html
<html>
<head>
<title>
forms
</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
h1 {
color:#c00;
font-family:sans-serif;
font-size:2em;
margin-bottom:0;
}
.table {
background-color: coral;
z-index: 999;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
position: absolute;
left: 50%;
bottom: 50%;
transform: translate(-50%, 50%);
}
</style>
</head>
<body background-color:"lightblue">
<center>
<form action = "http://127.0.0.1:8081/process_post" method =
"POST">
<h1>
PAT-Registration Form</h1>
<table style="width:55%;" class="table">
<tr>
<td><b>First Name:</b><br>
<input type="text" placeholder="Enter First
Name" name="first_name">
</td>
<td><b>Last Name:</b><br>
<input type="text" placeholder="Enter Last
Name" name="last_name">
</td>
</tr>
<tr>
<td colspan="2">
<b>Reg No.:</b><br>
<input type="text" placeholder="Enter Reg No"
name="regNo">
</td>
</tr>
<tr>
<td colspan="2">
<b>Sex</b><br>
<input type="radio" name="sex">Male<br>
<input type="radio" name="sex">Female
</td>
</tr>
<tr>
<td colspan="2">
<b>Date Of Birth</b><br>
<input type="date" name"dob">
</td>
</tr>
<tr>
<td colspan="2">
<b>Email address:</b><br>
<input type="email" placeholder="Enter Email"
name="mail"><br>
</td>
</tr>
<tr>
<td colspan="2">
<b>Year of Graduation</b><br>
<select name="year">
<option value="2021">2021</option>
<option value="2022" >2022</option>
<option value="2023" selected>2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<b>Select your Course</b><br>
<select name="Course">
<option value="Btech CSE">Btech CSE</option>
<option value="Int Mtech MID" selected>Int Mtech
MID</option>
<option value="Int Mtech SE">Int Mtech
SE</option>
<option value="Btech ECE">Btech ECE</option>
<option value="Btech EEE">Btech EEE</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<b>Any work Experience</b><br>
<input type="checkbox" name="Work">Yes<br>
<input type="checkbox" name="Work">No
</td>
</tr>

<tr>
<td colspan="2">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
Post.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/pat.html', function (req, res) {
res.sendFile( __dirname + "/" + "pat.html" );
})
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
first_name:req.body.first_name,
last_name:req.body.last_name,
regNo:req.body.regNo,
sex:req.body.sex,
dob:req.bodydob,

mail:req.body.mail,
year:req.body.year,
Course:req.body.Course,
Work:req.body.Work
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)


})
GET:

<!DOCTYPE html>
<html>
<head>
<title>PAT registration</title>
</head>
<body style= "background-color:pink;">
<forms>
<form action = "http://127.0.0.1:8081/process_get" method = "GET">
<b>PAT Registration</b>
<br>
Name:<br>
<input type="text" name="name" >
<br>
Reg_no:<br>
<input type="text" name="reg_no"><br>
Gender:<br>
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female<br>
<input type="radio" name="gender" value="other">Others<br>
Date_of_birth<br>
<input type="date" name="DOB"><br>
Email_id<br>
<input type="email" name="email"><br>
Phone_number<br>
<input type="tel" name="Phone" pattern="[0-9]{4}-[0-9]{3}-[0-9]{3}]">
<br>
CGPA<br>
<input type="text" name="cgpa"><br>
Address<br>
<address> <textarea name="address" row="30"
cols="20"></textarea><br>
</address>
<input type="submit" name="Submit">
<input type="reset" name="Reset">
</forms>
</body>
</html>
Server.js
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/index.html', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})
app.get('/process_get', function (req, res) {
// Prepare output in JSON format
response = {
name:req.query.name,
reg_no:req.query.reg_no,
gender:req.query.gender,
DOB:req.query.DOB,
email:req.query.email,
Phone:req.query.Phone,
cgpa:req.query.cgpa,
address:req.query.address

};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)


})

You might also like