-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (110 loc) · 3.31 KB
/
Copy pathindex.js
File metadata and controls
168 lines (110 loc) · 3.31 KB
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
var express = require("express")
var bodyparser = require("body-parser")
var mongoose = require("mongoose")
const app = express()
app.use(bodyparser.json())
app.use(express.static("public"))
app.use(bodyparser.urlencoded({
extended: true }))
mongoose.connect('mongodb://localhost:27017/wmart');
var db = mongoose.connection;
db.once("open",()=>console.log("Connected to Database"))
app.post("/login",(req,res)=>{
var name = req.body.name;
var email = req.body.email;
var phonenumber = req.body.phonenumber;
var password = req.body.password;
var data = {
"name": name,
"email":email,
"phonenumber":phonenumber,
"password":password,
"points":5
}
db.collection("users").insertOne(data,(err,collection)=>{
if(err){
throw err;
}
console.log("Record inserted successfully");
});
return res.redirect("index.html")
})
const userSchema = new mongoose.Schema({
email: String,
password: String,
});
const User = mongoose.model('users', userSchema);
app.post("/verify", async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email, password });
if (user) {
console.log("User found");
res.redirect(`/userpage?user=${JSON.stringify(user)}`);
} else {
res.redirect('/login.html');
}
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
app.post("/quizpoints", async (req, res) => {
fetch('/verify')
.then(res.redirect(`/userpage?user=${JSON.stringify(user)}`)
)
});
//products loading
const productSchema = new mongoose.Schema({
product_name: String,
product_points: String,
product_image:String,
});
const Product = mongoose.model('products', productSchema);
app.get("/products", async (req, res) => {
try {
console.log("Bringing the products from the database");
const pros = await Product.find();
res.send(pros);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get("/search", async (req, res) => {
const { search_val } = req.body;
try {
console.log("Searching required products from the database");
const pros = await Product.find();
res.send(pros);
} catch (error) {
res.status(500).json({ error: error.message });
}
})
const quizSchema = new mongoose.Schema({
question: String,
a: String,
b: String,
c: String,
d: String,
answer:String,
});
app.post("/view", async (req, res) => {
const productImage = req.query.data;
res.redirect(`/details?user=${JSON.stringify(productImage)}`)
});
app.get('/userpage', (req, res) => {
const userData = req.query.user;
if (userData) {
const user = JSON.parse(userData);
res.sendFile(__dirname + '/public/userpage.html');
} else {
res.status(400).send('User data not found');
}
});
//redirecting to the web page
app.get("/",(req,res)=>{
res.set({
"Allow-access-Allow-Origin":'*'
})
return res.redirect("login.html");
}).listen(3000);
console.log("Listening on port 3000");