INTRO JavaScript Framework
INTRO JavaScript Framework
Back End
Front End Node js Server
request(registration)
Server.js
Response(“registration.html”)
Creating New Project
New js file
app.js file
Port Number
To run the app.js
demofile1.html
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
Note:
you can at any time convert the html
based project to a node.js based
project by simply
• adding a node server to the project
folder (-npm init)
• and by creating the server.js code
at the root directory to handle all
pages calls and processes
Express Framework
• Express is a minimal and flexible Node.js web application framework that
provides a set of features to develop web and mobile applications.
• It facilitates the rapid development of Node based Web applications.
Following are some of the core features of Express framework −
• Allows to set up middlewares to respond to HTTP Requests.
• Defines a routing table which is used to perform different actions based on HTTP
Method and URL.
• Allows to dynamically render HTML Pages based on passing arguments to
templates.
Installing Express
app.listen(port)
Example:
When http://localhost:5000/home.html is requested
app.listen(5000);
Example GET Method (Login form)
Here is a simple example which passes two values using HTML FORM GET
method. We are going to use process_login router inside server.js to
handle this input login.html
<html>
<body> login.html
</body>
</html>
const port = 5000
var express = require('express');
call var app = express();
app.get('/login', function (req, res) {
res.sendFile( __dirname + "/" + "login.html" );
Response })
app.get('/process_login', function (req, res) {
first_name= req.query.fn;
last_name = req.query.ln;
call
res.end("my name is" + first_name + last_name);})
app.listen(port)
Response
<html>
<body>
</body>
</html>
Example:
console.log (first_name + last_name);
Database Connection
Installing WampSrever (Apache webserver and Mysql database server)
https://sourceforge.net/projects/wampserver/
ﻓﯾدﯾو ﺗﻧﺻﯾبwamp
https://youtu.be/nckzZSxiS_I
If you cannot install the latest version of wamp due to msvcr120.dll msvcr110.dll missing
error follow this link: https://www.youtube.com/watch?v=j4NNram24GM
Or install XAMPP https://www.apachefriends.org/index.html
Or use a remote MySQL Database Server mywindowshosting.com (free)
Default login:
Username: root
Password: null
API Fetching/Node server API
Sending data
http://localhost
Client Node
HTML Page API
2-Query string - ?id
3-Parameter - /id
API Fetching/Node server API methods
REST API Fetching Methods (POST, GET, PUT, DELETE)
http://localhos
GET (getting Single Record) Id t
app.get('/process_detail',
GET Single Record
fetch('http:// localhost:5000/process_detail?title=‘ + tt)
query string req. query.title;
app.post('/process_buy
POST json fetch("http:// localhost:5000/process_buy", options); …….
req.body.title,
app.put('/process_update’,
PUT json fetch("http://localhost:5000/process_update",option) …….
req.body.title,
app.delete('/process_delete’,
DELETE fetch("http:// localhost :5000/process_delete?title=" +tt,options query.title;
JavaScript API Fetching
options = {
method: ‘xx', Post/Put/Delete
headers: {
'Content-Type': 'application/json'
},
(JSON Object)
body: JSON.stringify(xx) Post,Put
}
Rest API Receiving data
http://localhost
Server.js
Client
HTML Page
JSON res.end(JSON.stringify(rows[0])) GET Single Record
await response.text();
text
res.end(" sucessful"); POST DELETE PUT
Insert.html
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" id="ti" class="form-control" required />
</div>
<div class="form-group">
<label for="description">Description:</label>
Note: if you <input type="text" class="form-control" name="description" id="de" required />
</div>
used fetch don’t <div class="form-group">
<label for="price">Price:</label>
use form or <input type="number" name="price" id="pr" class="form-control" required />
</div>
form submission <div class="form-group">
method <label class="control-label">Catagory</label>
<select class="form-control" name="cata" id="ca" >
<option value="0">Please select</option>
<option value="1">Computer Science</option>
<option value="2">Information Technology</option>
</select>
</div>
<button onclick="insert()">Insert</button>
<div id="res"></div>
<script>
async function insert() {
var ti = document.getElementById('ti').value;
var de = document.getElementById("de").value;
var pr = document.getElementById("pr").value;
var ca = document.getElementById("ca").value;
book = { title: ti, price: pr, description: de, cata: ca }
let options = {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(book) Local host IP address
}
let response = await fetch("http://127.0.0.1:5000/process_insert", options);
let message = await response.text();
document.getElementById("res").innerHTML = message;
}
</script>
</body>
</html>
Server.js var express = require('express');
1- npm install express
var app = express();
var mysql = require('mysql'); 2- npm install mysql
var cors = require('cors'); 3- npm install cors
app.use(cors({ origin: '*'})); CORS (Cross Origin Resource Sharing)
var con = mysql.createConnection({ To allow remote communication
host: "localhost",
user: "root",
password: "",
database: "test" });
app.use(express.json()); To listen to insert call
app.get('/insert', function (req, res) { To display the insert form
res.sendFile( __dirname + "/" + "insert.html" );
})
app.post('/process_insert',function (req, res) {
var ti=req.body.title;
var pr=req.body.price;
var ca=req.body.cata; To listen to process_ insert method call
var de=req.body.description;
var sql = "INSERT INTO book( title, description, price, cata) VALUES
('"+ti+"','"+de +"','"+pr+"' ,'"+ca+"')";
con.query(sql); To insert book into DB
res.end( "sucessfully added" );
})
app.listen(5000);
index.html
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
<div id="tb"></div>
</head>
<body>
<button class="btn btn-primary" onclick="getText('http://127.0.0.1:5000/process_index')">Show All Books <span
class="badge badge-light" id="nn"></span>
</button> Local host IP address
<script>
getnumber('http://127.0.0.1:5000/process_index')
async function getnumber(url) {
var response = await fetch(url);
var x = await response.json();
let num = x.length + 1;
document.getElementById("nn").innerHTML = num;
}
async function getText(url) {
var response = await fetch(url);
var x = await response.json();
ta = "<table border='1' class = 'animate__animated animate__fadeInDownBig table'>";
ta += "<tr><th>Id</th><th>Name</th><th>price</th> <th>cata</th> </tr>";
for (i = 0; i < x.length; i++) {
ta += "<tr> <td>" + x[i].Id + "</td><td>" + x[i].title + "</td><td>" +
x[i].price + "</td>" +
"<td>" + x[i].cata + "</td></tr>";
}
ta += "</table>";
document.getElementById("tb").innerHTML = ta;
}
Server.js
<html lang="en">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<body>
Book title: <input type="text" id="title" > <br>
<div id="res"></div>
<button type="button" onclick="deletetbook()">Delete</button>
<script>
async function deletetbook() {
var tt = document.getElementById('title').value;
let options = {
method: 'DELETE', To call process_delete method using API DELETE
headers: {
'Content-Type': 'application/json;charset=utf-8' },
}
let response = await fetch("http://127.0.0.1:5000/process_delete?title=" +tt,options);
let message = await response.text();
Swal.fire({ To print the success message
icon: 'success',
title: message,
showConfirmButton: false,
timer: 1500
}) }
</script></body></html>
Server.js
<script>
async function getbook() {
var dd = document.getElementById('ti').value;
var response = await fetch('http://127.0.0.1:5000/process_detail?title=' + dd);
var x = await response.json();
document.getElementById("de").value = x.description;
document.getElementById("pr").value = x.price;
document.getElementById("ca").value = x.cata
}
</script>
</body> </html>
Server.js
var tt = req.query.title;
var sql = "Select * from book where title= '" + tt + "' ";
con.query(sql, function (err, rows, fields) {
res.end(JSON.stringify(rows[0]))
}); To get book record based on its title
})
Security Note:
To prevent this SQL injection, you need to either use the escape() method as follows:
let sql = `SELECT * FROM todos WHERE title = ` + mysql.escape(tt);
change the ……………..
Detail.html code to <div class="form-group">
<label class="control-label">Catagory</label>
allow the update <select class="form-control" name="cata" id="ca">
<option value="0">Please select</option>
<option value="1">Computer Science</option>
<option value="2">Information Technology</option>
</select>
</div>
<button onclick="update()">Update</button>
<div id="res"></div>
<script> Add these codes
………………………………………….
async function update() {
var ti = document.getElementById('ti').value;
var de = document.getElementById("de").value;
var pr = document.getElementById("pr").value;
var ca = document.getElementById("ca").value;
book = { title: ti, price: pr, description: de, cata: ca }
let options = {
method: 'PUT',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(book)
}
let response = await fetch("http://127.0.0.1:5000/process_update", options);
let message = await response.text();
document.getElementById("res").innerHTML = message;
}
</script>
</body>
</html>
Server.js
YouTube Recording
https://youtu.be/6FxeaPnKV8o
<!DOCTYPE html> buy.html
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
</head>
<body>
<div class="row" id="demo"></div>
<div id="res"></div>
<script>
getText("http://127.0.0.1:5000/process_index");
async function getText(url) {
let response = await fetch(url);
let x = await response.json();
var ta = "";
for (i = 0; i < x.length; i++) {
ta += "<div class='col-md-3 animate__animated animate__fadeInUpBig '>" ;
ta += "<div class='card'>";
ta += "<div class='card-header bg-warning text-center'>" + x[i].Id + "</div>";
ta += "<div class='card-body'>" + x[i].title +"<br>Price: "+ x[i].price +"</div>";
ta += "<div class='card-footer'> <button class='btn btn-info' onclick=buy('" + x[i].price + "') >buy</button> </div>";
ta += "</div> </div>";
}
document.getElementById("demo").innerHTML = ta;
}
buy.html
var sql = "INSERT INTO orderbook( custid, total) VALUES ('"+cid+"','"+pr +"' )";
con.query(sql);
res.end( "sucessfully bought" );
})
Example:
File Upload When http://localhost:8081/index.html
is requested
npm install express-fileupload
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action = "process_file" method = "POST"
enctype = "multipart/form-data">
<div class="form-group">
<label>Select picture:</label>
<input type="file" name="file_pic" class="form-control" accept="image/png, image/jpeg">
</div>
<br />
<input type = "submit" value = "Upload File" />
</form>
</body></html>
var express = require('express');
To server static files such as images, CSS
var app = express(); files, and JavaScript files, use the
// express.static
app.use(express.static('img'));
//file uploading
const fileUpload = require('express-fileupload');
app.use(fileUpload());
// Create the img folder
app.get('/index.html', (req, res) => {
res.sendFile( __dirname + "/" + "index.html" );
})
//
app.post('/process_file', function (req, res) {
</div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" class="form-control" name="description" id="de" />
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" name="price" class="form-control" id="pr" />
</div>
<div class="form-group">
<label class="control-label">Catagory</label>
<select class="form-control" name="cata" id="ca">
<option value="0">Please select</option>
<option value="1">Computer Science</option>
<option value="2">Information Technology</option>
</select>
</div>
<div class="form-group">
<label>Select picture:</label>
<input type="file" id="ff" class="form-control" accept="image/png, image/jpeg">
</div>
<button onclick="insert()">Insert</button>
<div id="res"></div>
Add this code
<script>
document.getElementById("catl").innerHTML = ta;
}
</script>
</body></html>
49
Example: edit and search book with a pic
<html> <body>
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" class="form-control" id="ti" onchange="getbook()" /> </div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" class="form-control" name="description" id="de" />
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" name="price" class="form-control" id="pr" />
</div>
<div class="form-group">
<label class="control-label">Catagory</label>
<select class="form-control" name="cata" id="ca">
<option value="0">Please select</option>
<option value="1">Computer Science</option>
<option value="2">Information Technology</option> Add this code
</select>
</div>
<br><img src="" alt="" id="mm" width="150px"><br>
<div class="form-group">
<label>Select picture:</label>
<input type="file" id="ff" class="form-control" accept="image/png, image/jpeg">
</div>
<button onclick="update()">Update</button>
<div id="res"></div>
<script>
async function getbook() {
var dd = document.getElementById('ti').value;
var response = await fetch('http://127.0.0.1:5000/process_detail?title=' + dd);
var x = await response.json();
document.getElementById("de").value = x.description;
document.getElementById("pr").value = x.price;
document.getElementById("ca").value = x.cata;
document.getElementById("mm").src = 'http://127.0.0.1:5000/' + x.image;
}
let options = {
method: 'PUT',
body: data
}
let response = await fetch("http://127.0.0.1:5000/process_update", options);
let message = await response.text();
document.getElementById("res").innerHTML = message;
}
app.put('/process_update', function (req, res) {
var ti = req.body.title;
var pr = req.body.price;
var ca = req.body.cata;
var de = req.body.description; Add this code
var sql = "";
var uploadPath;
• html • process
• html • process
LocalStorage Session
• process
• html
• process
• html
Registration
<html>
<body background="light pink">
<form id="RegForm" action="process_registration" method="post">
<fieldset>
<legend>Registration</legend>
<p>
<label>Username </label><br><br>
<input type="text" name="na" required placeholder="Enter your username"/>
</p>
<p>
<label>Password </label><br><br>
<input type="password" name="pa" id="password1" required placeholder="Enter password"/>
</p>
<p>
<label>Confirm Password </label><br><br>
<input type="password" id="password2" required placeholder="Re-ente password"/>
</p><br>
<p>
<label>email </label><br><br>
<input type="email" name="em" required placeholder="enter email"/>
</p><br>
<input type="submit" value="Submit"/>
</fieldset>
</form>
</body>
</html>
app.get('/registration', function (req, res) {
})
app.post('/process_registration',function (req, res) {
var nn=req.body.na;
var pp=req.body.pa;
var ee=req.body.em;
var sql = "INSERT INTO users( username, password, role, email) VALUES ('"+nn+"','"+pp +"','customer'
,'"+ee+"')";
con.query(sql);
res. redirect ('login');
})
For using Sessions
npm install express-session
<body>
<div class="mx-auto mt-5 bg-light p-5" style="width: 30%;">
<form action = "loginprocess" method = "POST" >
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" required />
</div>
<div class="form-group">
<label for="email">Password:</label>
<input type="password" class="form-control" name="pass" placeholder="Your password" required />
</div>
<button type="submit" class="btn btn-info">Log in</button>
</div>
</form>
</body>
var express = require('express'); npm install express
var app = express();
var mysql = require('mysql'); npm install mysql
var cors = require('cors'); npm install cors
app.use(cors({ origin: '*'}));
var con = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "test" });
npm install express-fileupload
app.use(express.json()); npm install express-session
app.use(express.static('img'));
const fileUpload = require('express-fileupload');
app.use(fileUpload());
To generate a unique 64 random
var sessions = require('express-session');
app.use(sessions({ secret: "thisismysecrctekeyfhrgfgrfrty84fwir767", saveUninitialized:true, resave: false })); hexadecimal secret key use this web site
https://www.grc.com/passwords.htm,
app.use(express.urlencoded({ extended: true })); Using secret key you could change it
app.post('/loginprocess',function (req, res) {
var nn = req.body.name;
var pp = req.body.pass;
var sql = "select * from users where username = '"+ nn+"' and password = '"+pp+"' " ;
con.query(sql,function (err, rows) {
if (rows.length > 0) { Saving data in sessions in case a valid user
session=req.session;
session.userId=rows[0].Id;
session.username=rows[0].username;
session.userrole= rows[0].role;
res. redirect ('home');
}
else
res.end( "wrong username or password" ); });
})
app.get('/login',function (req, res) {
res.sendFile( __dirname + "/" + "login.html" );
})
To make buy a secure page for customers only
You need to change your email account security setting to allow sending emails by
an application
e.g. For gemail
https://youtu.be/mg6fxvgEuec
app.post('/process_registration',function (req, res) {
var nn=req.body.na;
var pp=req.body.pa;
var ee=req.body.em;
var sql = "INSERT INTO users( username, password, role, email) VALUES ('"+nn+"','"+pp +"','customer' ,'"+ee+
con.query(sql);
emto= "user@hotmail.com";
title= "successful Registration";;
message="Thank you for your registration";
var nodemailer = require('nodemailer'); To send welcome email after registration
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'aimanturani@gmail.com',
pass: 'xxxxx'
},
tls: {rejectUnauthorized: false }
});
var mailOptions = {
from: 'aimanturani@gmail.com',
to: emto,
subject: title,
text: message
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res. redirect ('login');
All CRUD operation in a single page using modal effects
<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">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body><br>
<button type="button" onclick="insertbook()" class="btn btn-success" data-toggle="modal"
data-target="#myModal">Insert new Book</button> <br>
<div class="modal fade" id="myModal" role="dialog"> To show the myModal when clicked
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Manage Book</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" name="title" class="form-control" id="ti" />
</div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" class="form-control" id="de" />
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" class="form-control" id="pr" />
</div>
<div class="form-group">
<label class="control-label">Catagory</label>
<select class="form-control" id="ca">
<option value="0">Please select</option>
<option value="1">Computer Science</option>
<option value="2">Information Technology</option>
</select>
</div>
<br><img src="" alt="" id="mm" width="150px"><br>
<div id="picd" class="form-group">
<label>Select picture:</label>
<input type="file" id="ff" class="form-control" accept="image/png, image/jpeg">
</div>
</div>
<div class="modal-footer">
<button id="insert" class="btn btn-info" data-dismiss="modal" onclick="insert()">Insert</button>
<button id="edit" class="btn btn-info" data-dismiss="modal" onclick="update()">Update</button>
<button id="del" class="btn btn-info" data-dismiss="modal" onclick="delet()">Delete</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div> </div> </div></div><br>
<table class="table table-striped" id="demo"></table>
<script>
document.getElementById("edit").style.display = "none"; To control the visibility of modal buttons and the file uploader
document.getElementById("picd").style.display = "none";
document.getElementById("insert").style.display = "none";
document.getElementById("del").style.display = "none";
getList("http://127.0.0.1:5000/process_index");
let options = {
method: 'PUT',
body: data
}
let response = await fetch("http://127.0.0.1:5000/process_update", options);
getList("http://127.0.0.1:5000/process_index");
}
async function delet() {
document.getElementById("del").style.display = "none";
if (confirm("are you sure!") == true) { To confirm the deletion
var tt = document.getElementById('ti').value;
let options = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
}
let response = await fetch("http://127.0.0.1:5000/process_delete?title=" + tt, options);
getList("http://127.0.0.1:5000/process_index");
}
}
</script>
</body>
</html>
Multi Line Entry Example
Shopping Cart
Multi Line Entry Example
Shopping Cart
var data = {
“customer”:”Aiman”,
“item” : [
{“name":“book1",”price”:100,“quant":2},
{“name":“book2",” price”:100,““quant":1},
],
}
cart.html
<html>
<body>
Customer Number: <input type='text' id='cnm'> <br> <br>
Enter Book Name <input type="text" id="tt" onchange="getprice()">
Price <label id="lbl1"></label>
enter quantity <input type="number" id="num" size="5">
<button onclick="add()">Buy</button>
<div id="res"></div>
<br>
<table border="1" id="tbl"></table>
<button onclick="checkout()">Check out</button>
<script>
pp = "";
cartItems = [];
function add() {
item = { name: tt.value, price: lbl1.innerHTML, quant: num.value, };
cartItems.push(item);
display();
lbl1.innerHTML = ""; tt.value = ""; num.value = "";
}
async function getprice() {
var dd = document.getElementById('tt').value;
var response = await fetch('http://127.0.0.1:5000/process_detail?title=' + dd);
var x = await response.json();
document.getElementById("lbl1").innerHTML = x.price;
}
function display() {
var ta = "My Shopping Cart<tr><th>Name</th><th>Price</th> <th>quantity</th><th></th> </tr>";
for (var i = 0; i < cartItems.length; i++) {
ta += "<tr><td>" + cartItems[i].name + "</td><td>" + cartItems[i].price + "</td> <td>" +
cartItems[i].quant + " </td> <td><button class='btn btn-info' onclick=del('" + cartItems[i].name + "')
>Delete</button> </td></tr>";
}
document.getElementById("tbl").innerHTML = ta;
}
function del(nn) {
for (var i = 0; i < cartItems.length; i++) {
if (cartItems[i].name == nn) {
cartItems.splice(i, 1);
}
}
display();
}
async function checkout() {
cn = document.getElementById("cnm").value;
order = { customer: cn, itemlines: cartItems }
let options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(order)
}
let response = await fetch("http://127.0.0.1:5000/process_cart", options);
let myText = await response.text();
document.getElementById("res").innerHTML = myText;
document.getElementById("cnm").value = "";
document.getElementById("tbl").innerHTML = "";
}
</script></body></html>
app.get('/cart.html', function (req, res) {
res.sendFile(__dirname + "/" + "cart.html");
})
app.post('/process_cart', function (req, res) {
var cn = req.body.customer;
var orline = req.body.itemlines;
var sql = "INSERT INTO orderbook( custid, total) VALUES ('" + cn + "','0' )";
con.query(sql);
var sql = "Select * from orderbook where custid= '" + cn + "' order by id desc ";
con.query(sql, function (err, rows, fields) {
var ordid = rows[0].id; Getting the created order Id
tot = 0;
orline.forEach(myFunction);
function myFunction(bk) {
sql = "UPDATE orderbook SET total = '" + tot + "' where Id ='" + ordid + "' ";
con.query(sql);
res.end("sucessfully bought"); To update the order total value
});
Using EJS Template
Sending Data From Server to html file (without using fetch)
Detail.html
x
Server Data
Cannot send data to html pages
(without fetch)
Detail.ejs
Server Data
id Can send data to ejs pages
Using EJS Template
ejs page
Convert
ejs page
to html
calling
page
Using EJS Template Engine in Nodejs Project
var express = require('express');
var app = express();
var mysql = require('mysql');
1- npm install express
var cors = require('cors');
app.use(cors({ origin: '*'})); 2- npm install mysql
var con = mysql.createConnection({
host: "localhost", 3- npm install cors
user: "root", 4- npm install express-fileupload
password: "",
database: "test" });
app.use(express.json());
5- npm install ejs
app.use(express.static('img'));
//file uploading
const fileUpload = require('express-fileupload');
app.use(fileUpload());
app.set('views', './views');
app.set('view engine', 'ejs');
To set up this middleware:
views is the folder where our all pages (ejs files) will be kept.
to setup the view engine to use ejs template
<html>
<%- include('head')-%>
<body>
<div class="form-group">
Title:<%= ti %>
</div>
<div class="form-group">
Description: <%= de %>
</div>
<div class="form-group">
Price:<%= pr %>
</div>
<div class="form-group">
<img src=<%= im %> height="150px">
</div>
<a class='btn btn-info' href='manage' >Go Back to Book List</a>
</body>
</html>