-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (122 loc) · 3.85 KB
/
index.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
const express = require('express')
const path = require('path');
const ejs = require("ejs")
const app = express()
const port = 3000
//create middleware for passing data----------------------
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, "css")))
app.use(express.static(path.join(__dirname, "image")))
// Set View Engion----------------------------------------------------------
app.set("view engine", "ejs");
// Models...................................................................
const userModel = require("./database_module").userModel;
const productModel = require("./database_module").productModel;
const { RSA_NO_PADDING } = require('constants');
//APIs for home-page
app.get('/', (req, res) => {
try {
res.sendFile(path.join(__dirname, "templates/home.html"));
}
catch (error) {
res.send(error);
}
})
//APIs for registration form----------------------------------------------------------------
app.get("/register", (req, res) => {
res.sendFile(path.join(__dirname, "templates/register.html"))
})
app.post("/register", async (req, res) => {
try {
const data = req.body;
console.log(data)
let newRecord = new userModel({
"name": req.body.name,
"mobile": req.body.mobile,
"email": req.body.email,
"password": req.body.password,
"utype": req.body.utype,
"gender": req.body.gender,
"address": req.body.address
});
newRecord.save();
res.sendFile(path.join(__dirname, "templates/login.html"))
}
catch (error) {
res.send("Not Submiitted")
}
})
//APIs for login form---------------------------------------------------------------------------
app.get("/login", (req, res) => {
res.sendFile(path.join(__dirname, "templates/login.html"))
})
app.post("/login/", async (req, res) => {
try {
const entered_email = req.body.email;
const entered_password = req.body.password;
console.log(entered_email, entered_password)
const fetched_data = await userModel.findOne({ "email": entered_email });
console.log(fetched_data)
const product = await productModel.find();
console.log(product[0]["title"])
if (fetched_data.password == entered_password) {
res.render("user-dashboard", {
data: fetched_data,
product: product
});
}
else {
res.sendFile(path.join(__dirname, "templates/register.html"))
}
}
catch (error) {
res.send(error + "Account Not Found")
}
//console.log(data["Name"])
})
// APIs for product--------------------------------------------------------------------------------
//user-dashboard------------------------------------------------
app.get("/user-dashboard", async (req, res) => {
try {
let data = await productModel.find();
console.log("Hello World..");
res.render("user-dashboard", {
product: data
});
}
catch (error) {
res.send(error);
}
})
// Profile------------------------
app.get("/profile", (req, res) => {
try {
res.render("profile")
}
catch (error) {
res.send(error)
}
});
// Orderd Product---------------------------------------------------------
app.get("/ordered_product", (req, res) => {
try {
res.render("ordered_product")
}
catch (error) {
res.send(error);
}
})
// Transaction History---------------------------------------------------------
app.get("/transaction_history", (req, res) => {
try {
res.send("Hello World" + req.params.name)
}
catch (error) {
res.send(error);
}
})
// Strat Local host port
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})