-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (109 loc) · 3.42 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
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
}))
// You should put the server URL here
mongoose.connect('localhost:3000')
var db = mongoose.connection;
db.on('error',()=>console.log("Error in Connecting to Database"));
db.once('open',()=>console.log("Connected to Database"))
app.post("/sign_up",(req,res)=>{
var name = req.body.name;
var email = req.body.email;
var phno = req.body.phno;
var password = req.body.password;
db.collection('users').findOne({ email: email }, (err, user) => {
if (err) {
throw err;
}
if (user) {
res.send('<script>alert("Email already exists. Please use a different email."); window.location.href="/"</script>');
} else {
var data = {
"name": name,
"email": email,
"phno": phno,
"password": password
}
db.collection('users').insertOne(data, (err, collection) => {
if (err) {
throw err;
}
console.log("Record Inserted Successfully");
return res.redirect('signup_sucess.html');
});
}
});
});
app.get("/update", (req, res) => {
res.sendFile(__dirname + "/update.html");
});
app.post("/update_user", (req, res) => {
var email = req.body.email;
var newPassword = req.body.newPassword;
var newEmail = req.body.newEmail;
var newName = req.body.newName;
var newPhno = req.body.newPhno;
var query = { email: email };
var update = {
$set: {
password: newPassword,
email: newEmail,
name: newName,
phno: newPhno
}
};
db.collection('users').updateOne(query, update, (err, result) => {
if (err) {
throw err;
}
console.log("Record Updated Successfully");
return res.redirect('update_success.html');
});
});
app.get("/delete", (req, res) => {
res.sendFile(__dirname + "/delete.html");
});
app.post("/delete_user", (req, res) => {
var emailToDelete = req.body.email;
var query = { email: emailToDelete };
db.collection('users').deleteOne(query, (err, result) => {
if (err) {
throw err;
}
if (result.deletedCount > 0) {
console.log("Record Deleted Successfully");
return res.redirect('delete_success.html');
} else {
console.log("No matching record found for deletion");
return res.redirect('delete_failure.html');
}
});
});
app.get("/dashboard", (req, res) => {
res.sendFile(__dirname + "/dashboard.html");
});
app.get("/get_user", (req, res) => {
console.log("Fetching user data...");
db.collection('users').find({}).toArray((err, users) => {
if (err) {
console.error('Error fetching user data:', err);
res.status(500).send('Internal Server Error');
return;
}
console.log('User data:', users);
res.json(users);
});
});
app.get("/",(req,res)=>{
res.set({
"Allow-access-Allow-Origin": '*'
})
return res.redirect('index.html');
}).listen(3000);
console.log("Listening on PORT 3000");