Mobile App
Mobile App
5. Create a NodeJS server using Express that creates, reads, updates and deletes
students details and stores them in MongoDB database. The information about the user
should be obtained from a HTML form.
Aim:
To write a program for Create a NodeJS server using Express that creates, reads,
updates and deletes students details and stores them in MongoDB database. The information
about the user should be obtained from a HTML form.
Procedure:
Should go towards start menu-> cmd, It’s a way of opening windows command prompt,
then select project directory and put the command code . , it’s open the visual studio code
with the chosen directory.
Initialize the node package manager set up with help of npm init –y command. This
command is generate package.json file automatically. Which is contain information
related our project.
To Install needed dependencies with the help of npm install package name command.
To prepare server side JavaScript in server.js file. This file is first execution file of our
project that’s add a command into start property inside of package.json.
”start”:”nodemon server.js”
Server side code must have express module imported statement. It’s manage the all the
express framework tasks. At the same time need another one module which is mongodb.
It’s helps establish the connection between mongodb and node js server.
const app = express() this statement gives express framework control to app variable.
mongo.connect() function is start the connection it has two arguments which is mongodb
url and one callback function.
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Code:
Package.json
{
"name": "5",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"express-handlebars": "^6.0.6",
"hbs": "^4.2.0",
"mongodb": "^4.8.1",
"nodemon": "^2.0.19"
}
}
index.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
width: 80%;
margin: 10px 10% auto;
}
table,tr{
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1>Student Details</h1>
</center>
<div align="right" > <a href="/add">New Record</a> </div>
<table width="100%" border="1" >
<thead>
<tr>
<th>Roll No</th>
<th>Name</th>
<th>DOB</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
<tbody>
{{#each data}}
<tr>
<td>{{this.rollno}}</td>
<td>{{this.name}}</td>
<td>{{this.dob}}</td>
<td>{{this.email}}</td>
<td>
<a href="edit/{{this.rollno}}">edit</a>
<a href="delete/{{this.rollno}}">delete</a>
</td>
</tr>
{{/each}}
</tbody>
</table>
</body>
</html>
add.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/create" method="post" >
<p> Roll No</p>
<input type="text" name="rollno" required>
<p>Name</p>
<input type="text" name="name" required>
<p>DOB</p>
<input type="date" name="dob" required>
<p>Email</p>
<input type="email" name="email" required>
<input type="submit" value="submit">
</form>
</body>
</html>
edit.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{#each data}}
<form action="/update/{{this.rollno}}" method="post" >
<p> Roll No</p>
<input type="text" value="{{this.rollno}}" disabled name="rollno"
required>
<p>Name</p>
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
server.js
const { response } = require('express')
const express = require('express')
const app = express()
const { engine } = require('express-handlebars')
app.use(express.urlencoded({extended:true}))
app.use(express.json({extended:true}))
app.set('views','./views')
app.set('view engine','hbs')
app.use('handlebars',engine({express:'hbs'}))
app.get('/add',(req,res)=>{
res.render('add')
})
app.get('/edit/:rollno',(req,res)=>{
console.log(req.params.rollno);
mongo.connect(url,(err,db)=>{
let dbo = db.db('mydb')
dbo.collection('student').findOne({"rollno":req.params.rollno},(err,result)
=>{
console.log(result);
db.close()
res.render('edit',{data:[result]})
})
})
})
app.post('/update/:rollno',(req,res)=>{
mongo.connect(url,(err,db)=>{
let dbo = db.db('mydb')
dbo.collection('student').updateOne({"rollno":req.params.rollno},{
$set:{'rollno':req.params.rollno,
'name':req.body.name,
'dob':req.body.dob,
'email':req.body.email
}
},(err,result)=>{
db.close()
res.redirect('/')
})
})
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
})
app.post('/create',(req,res)=>{
mongo.connect(url,(err,db)=>{
let dbo = db.db('mydb')
dbo.collection('student').insertOne(req.body,(err,result)=>{
db.close()
res.redirect('/')
})
})
})
app.get('/',(req,res)=>{
mongo.connect(url,(err,db)=>{
let dbo = db.db('mydb')
dbo.collection('student').find({}).toArray((err,result)=>{
db.close()
res.render('index',{data:result})
})
})
})
app.get('/delete/:rollno',(req,res)=>{
mongo.connect(url,(err,db)=>{
let dbo = db.db('mydb')
dbo.collection('student').remove({"rollno":req.params.rollno},(req,result)=
>{
db.close()
res.redirect('/')
})
})
})
app.listen(3000,console.log('server start'))
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Output:
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal
fffffffffffffoooooooooooadfadfakflaksksdfafalskjfal