-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
266 lines (234 loc) Β· 9.28 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
require("./src/conn.js");
const students = require("./src/db/studentdataschema.js");
const studentpass = require("./src/db/studentpassschema.js");
const { error } = require("console");
const bcrypt = require("bcrypt");
// use('collegeDB');
// const studentauthentications = require('./studentPasswordSchema.js');
const PORT = process.env.PORT || 5000;
const app = express();
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.json());
app.use(express.urlencoded()); // this is basically to decode data send through html page
const route = express.Router();
const frontend = path.join(__dirname, "public");
console.log(frontend);
// Middleware for parsing request body
// app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.json());
app.use(express.static(frontend));
// Middleware for logging requests
const reqFilter = (req, resp, next) => {
// console.log(`Request made to ${req.url} with method ${req.method}`);
next();
};
app.use(reqFilter);
//API GET ROUTS
app.get("", (req, resp) => {
resp.sendFile(`${frontend}/index.html`);
});
app.get("/about", (req, resp) => {
resp.sendFile(`${frontend}/html/about.html`);
});
app.get("/contact", (req, resp) => {
resp.sendFile(`${frontend}/html/contact.html`);
});
app.get("/help", (req, resp) => {
resp.sendFile(`${frontend}/html/help.html`);
});
app.get("/blog", (req, resp) => {
resp.sendFile(`${frontend}/html/blog.html`);
});
app.get("/courses", (req, resp) => {
resp.sendFile(`${frontend}/html/courses.html`);
});
app.get("/studentlogin", (req, resp) => {
resp.sendFile(`${frontend}/html/stdntlgn.html`);
});
app.get("/student-signup", (req, resp) => {
resp.sendFile(`${frontend}/html/signup.html`);
});
app.get("/register", (req, resp) => {
resp.render(`registration`); //rendering registration.ejs file
});
app.get("/forgotpassword", (req, resp) => {
resp.render(`forgotpass`); //rendering forgotpass.ejs file
});
app.get("/home", (req, resp) => {
resp.render(`home`); //rendering home.ejs file
});
app.get("/deshboard", (req, resp) => {
resp.render(`deshboard`); //rendering deshboard.ejs file
});
app.get("/attendance", (req, resp) => {
resp.render(`attendance`); //rendering attendance.ejs file
});
app.get("/user", (req, res) => {
// Replace with logic to get user data based on session or login
const user = users[0];
res.render("user", { user: user, announcements: announcements });
});
// .......................Node.js code to handle form submissions and save data to MongoDB:................................
app.post("/register", async (req, res) => {
let studentdata = new students(req.body);
console.log(studentdata);
let result = await studentdata.save();
// Render the registration success page with the registration number
// res.render('registration', { RegistrationNo: studentdata.RegistrationNo });
});
// app.post('/update', async (req, res) => {
// const username = req.body.username;
// const user = await studentpass.findOne({username});
// if (user) {
// res.status(409).json({ error: "User exist redirecting to login page" });
// }else{
// let studentPassword = new studentpass(req.body);
// let result = await studentPassword.save();
// res.status(200).json({ message: "Password updated redirecting to login page" });
// // Render the registration success page with the registration number
// // res.render('registration', { RegistrationNo: studentdata.RegistrationNo });
// }
// });
app.post("/update", async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user already exists
const existingUser = await studentpass.findOne({ username });
if (existingUser) {
return res
.status(409)
.json({ error: "User already exists. Redirecting to login page" });
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10); // 10 is the salt rounds
// Create a new studentpass document with hashed password
const newStudentPassword = new studentpass({
username: username,
password: hashedPassword,
});
// Save the new studentpass document
await newStudentPassword.save();
return res.status(200).json({
message:
"Shabhash mere sher! Password updated. Redirecting to login page",
});
} catch (error) {
console.error("Error:", error);
return res
.status(500)
.json({ error: "Internal server error, Hey Prabhu! Ye kya hua?" });
}
});
// app.post('/login', async (req, res) => {
// const { username, password } = req.body;
// try {
// // Check if the user exists in the database
// const user = await studentpass.findOne({username});
// // console.log(user.password === password && user.username == username);
// if (user) {
// if(user.username == username && user.password === password){
// // User found, grant access to home page
// res.status(200).json({ message: "Login successful" , redirectTo: "/home"});
// }else if(user.username == username && user.password !== password){
// // Password does not match, return error message
// res.status(401).json({ error: "Wrong password" });
// }else {
// // User not found, return error message
// res.status(404).json({ error: "User not found" });
// }
// }
// } catch (error) {
// // Error occurred during database query
// res.status(500).json({ error: "Internal server error" });
// }
// });
app.post("/login", async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user exists in the database
const user = await studentpass.findOne({ username });
if (user) {
// Compare hashed password
const isPasswordMatch = await bcrypt.compare(password, user.password);
if (isPasswordMatch) {
// Passwords match, grant access to home page
return res
.status(200)
.json({ message: "Login successful", redirectTo: "/home" });
} else {
// Password does not match, return error message
// return res.status(401).json({ error: "Wrong username or password" });
return res.status(401).json({
error:
"Wrong password π Ex ka number yad rehta hai tumhe, par apna password nahi",
});
}
} else {
// User not found, return error message
return res.status(404).json({ error: "User not found" });
}
} catch (error) {
// Error occurred during database query
console.error("Error:", error);
return res
.status(500)
.json({ error: "Internal server error, Hey Prabhu! Ye kya hua?" });
}
});
// ........................................................................................
// Replace with your database connection and data fetching logic
// const users = [
// {
// name: "John Doe",
// studentID: "123456",
// email: "[email protected]",
// program: "Computer Science",
// classes: [
// { code: "CSC101", name: "Introduction to Programming", day: "M/W/F", time: "10:00 AM" },
// { code: "MATH120", name: "Calculus I", day: "T/Th", time: "2:00 PM" },
// ],
// },
// ];
// const announcements = [
// { title: "Important Update: Online Exam Schedule", date: "2024-05-10", message: "The online exam schedule for the upcoming semester has been released. Please check your email for details." },
// { title: "Library Resources for Research Projects", date: "2024-05-08", message: "The library offers a wide range of resources for your research projects. Attend our workshop on effective research strategies on May 15th." },
// ];
// Start the servern
app.use("/", route);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// ---------FOLDER STRUCTURE -------
// EDUFOLDER/
// β
// β
// β
// βββ Server/ (Backend-related code)
// β βββ node_modules/ (Auto-generated by npm/yarn)
// β βββ src/ (Backend source code)
// β β βββ db/ (Database related files)
// β β βββ models/ (Database models)
// β β βββ app.js (Main backend file)
// β | |___ config.js (Database configuration)
// β | |___ studentdataschema.js (Schema for student data)
// β βββ .gitignore (Git ignore file for backend)
// β βββ package.json (Backend dependencies and scripts)
// β βββ ... (Other backend-related files)
// β
// βββ src/ (Frontend-related code)
// | βββ frontend/ (Frontend source code)
// | β βββ API/ (API related files)
// | β βββ images/ (Images used in frontend)
// | β βββ html/ (HTML files for frontend)
// | β βββ css/ (CSS stylesheets)
// | β βββ index.js (Entry point for frontend)
// | βββ .gitignore (Git ignore file for frontend)
// | βββ package.json (Frontend dependencies and scripts)
// | βββ ... (Other frontend-related files)
// |
// βββ views/ (EJS files)