-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
203 lines (169 loc) · 5 KB
/
server.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const express = require("express");
const path = require("path");
const bodyparser = require("body-parser");
const session = require("express-session");
const { v4: uuidv4 } = require("uuid");
const router = require("./router");
const dashboard = require("./dashboard");
const rider = require("./rider");
const payment = require("./payment");
const rate = require("./rate");
const provider = require("./provider");
const receipt = require("./receipt");
const app = express();
const port = process.env.PORT || 3000;
//DATABASE
var db = require("./db");
// var mysql = require("mysql");
// var db = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "",
// database: "ridesharing",
// multipleStatements: true
// });
// db.connect(function (error) {
// if (error) {
// console.log("Error Connecting to DB");
// } else {
// console.log("successfully Connected to DB");
// }
// });
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
// load static assets
app.use("/static", express.static(path.join(__dirname, "public")));
app.use("/assets", express.static(path.join(__dirname, "public/assets")));
app.use(
session({
secret: uuidv4(), // '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
resave: false,
saveUninitialized: true,
})
);
app.use("/route", router);
app.use("/dashboard", dashboard);
app.use("/rider", rider);
app.use("/payment", payment);
app.use("/rate", rate);
app.use("/provider", provider);
app.use("/receipt", receipt);
// home route
// app.get('/', (req, res) =>{
// res.render('base', { title : "Login System"});
// })
// //////////////////////
app.get("/", (req, res) => {
res.render("rslogin", { title: "Login System" });
});
app.post("/", (req, res) => {
console.log(req.body);
var id = encodeURIComponent(req.body.id.toString());
var provider = req.body.flexRadioDefault;
if (provider == "on") {
let sql1 = "SELECT * FROM providers WHERE id=?";
let query = db.query(sql1, [id], (err, result1) => {
if (err) throw err;
if (req.body.password == result1[0].password) {
console.log("PASSWORD MATCHED");
res.redirect("/dashboard/provider/?id=" + id);
} else {
res.send("Password incorrect");
}
});
} else {
let sql1 = "SELECT * FROM rider WHERE id=?";
let query = db.query(sql1, [id], (err, result1) => {
if (err) throw err;
console.log(result1);
console.log("result1", result1);
console.log(typeof req.body.password, typeof result1[0].password);
console.log(req.body.password.length, result1[0].password.length);
console.log(req.body.password, result1[0].password);
if (req.body.password == result1[0].password) {
console.log("PASSWORD MATCHED");
res.redirect("/dashboard/rider/?id=" + id);
} else {
res.send("Password incorrect");
}
});
}
});
app.get("/logout", (req, res) => {
res.redirect("/");
});
app.get("/register", (req, res) => {
res.render("registration", { title: "Login System" });
});
app.post("/register", (req, res) => {
let details = {
id: req.body.id,
name: req.body.name,
address: req.body.address,
pickup: req.body.pickup,
department: req.body.department,
password: req.body.password,
email: req.body.email,
phone: req.body.phone,
};
console.log(details);
let details2 = {
...details,
vehicleNum: req.body.Vnum,
vehicleType: req.body.Vtype,
vehicleColor: req.body.Vcolor,
vehicleModel: req.body.Vmodel,
driverName: req.body.DriverName,
driverNum: req.body.DriverNum,
};
var provider = req.body.flexRadioDefault;
if (provider == "on") {
let sql = "INSERT INTO providers SET ?";
let query = db.query(sql, details2, (err, result) => {
if (err) throw err;
console.log(result);
});
res.redirect("/");
} else {
/// Pushing rider data into database
let sql = "INSERT INTO rider SET ?";
let query = db.query(sql, details, (err, result) => {
if (err) throw err;
console.log(result);
});
res.redirect("/");
}
});
////////////////////// DATABASE ///////////////////
app.get("/create", (req, res) => {
let sql =
"CREATE TABLE posts (id int AUTO_INCREMENT, title VARCHAR(255), PRIMARY KEY (id))";
db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
res.send("psots table created");
});
});
app.get("/insert", (req, res) => {
let details = {
id: 201012655,
name: "Ahnaf",
address: "Gulshan",
pickup: "Gloria Jeans, Gulshan 1",
department: "Microbio",
password: "mimi",
email: "[email protected]",
phone: 4555555556,
};
let sql = "INSERT INTO rider SET ?";
let query = db.query(sql, details, (err, result) => {
if (err) throw err;
console.log(result);
// res.send('Rider info added')
});
});
// module.exports = {db};
app.listen(port, () => {
console.log("Listening to the server on http://localhost:3000");
});