-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
107 lines (81 loc) · 2.67 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const express = require('express');
const path = require('path');
const app = express();
const con = require("./connection");
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs'); //view file
//app.use(express.static("public")); // Style.css
app.set('views', path.join(__dirname, '/views'));
//render port 8000
app.listen(3500, function(){
console.log("Server is Running");
});
// Read Data from Database
app.get("/", function(req, res) {
var sql = 'select * from employee;';
con.query(sql, function(error, result) {
if (error) throw error;
res.render("display", {test: result });
});
});
// Route for Inserting Data
app.post("/", function(req, res) {
var id = req.body.id;
var name = req.body.name;
var email = req.body.email;
var phone = req.body.phone;
var address = req.body.address;
console.log(name, email);
var sql = 'INSERT INTO employee (id,name, email,phone, address) values (?)';
var values = [id,name, email,phone, address];
con.query(sql, [values], function(err, result) {
if (err) throw err;
console.log("Data Uploaded.");
res.redirect("/");
});
});
// //Route for reading Data
// app.get("/", function(req, res) {
// var sql = 'select * from employee;';
// con.query(sql, function(error, result) {
// if (error) throw error;
// res.render("display", {test: result });
// });
// });
app.get("/update", function(req, res) {
con.connect(function(error) {
if (error) console.log(error);
var sql = "select * from employee where id = ?;";
var id = req.query.id;
con.query(sql,[id], function(error, result) {
if(error)console.log(error);
res.render('update',{ test: result });
});
});
});
app.post("/updateData", function(req, res) {
var name = req.body.name;
var email = req.body.email;
var phone = req.body.phone;
var address = req.body.address;
var id = req.body.id;
var sql = "update employee set name=? , email=?, phone=?, address=? where id =?;";
con.query(sql, [name, email,phone, address, id], function(error, result) {
if (error) console.log(error);
console.log("data updated");
res.redirect("/");
});
});
app.get("/delete", function(req, res) {
con.connect(function(err) {
if (err) console.log(err);
var sql = "delete from employee where id=?";
var id = req.query.id;
con.query(sql, [id], function(error, result) {
if (error) console.log(error);
res.redirect("/")
});
});
});