-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
141 lines (118 loc) · 4.22 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
// Import required libraries/modules
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const path = require("path");
// Create an instance of Express
const app = express();
dotenv.config(); // Load environment variables again (repeated)
// Define the port where the server will listen
const port = process.env.PORT || 3000;
// Retrieve MongoDB connection details from environment variables
const username = process.env.MONGODB_USERNAME || "";
const password = process.env.MONGODB_PASSWORD || "";
const dbname = process.env.MONGODB_DBNAME || "";
// console.log('-----------------------------------------------------------------')
// console.log("Username:", username);
// console.log("Password:", password);
// console.log("Database:", dbname);
// console.log('-----------------------------------------------------------------')
// Connect to MongoDB using mongoose
mongoose.connect(
`mongodb+srv://${username}:${password}@cluster0.j4relx6.mongodb.net/${dbname}?retryWrites=true&w=majority`
);
// Define the schema for user registration
const registrationSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
number: String,
});
// Create a model based on the registration schema
const Registration = mongoose.model("Registration", registrationSchema);
// Configure Express to use bodyParser for parsing request bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Middleware for serving static files from the 'public' directory
app.use(express.static(path.join(__dirname, "public")));
// Define routes
// Home page route
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// Registration page route
app.get("/registerPage", (req, res) => {
res.sendFile(__dirname + "/pages/index.html");
});
// Login Page route
app.get("/loginPage", (req, res) => {
res.sendFile(__dirname + "/pages/login.html");
});
// Registration endpoint
app.post("/register", async (req, res) => {
try {
const { name, email, password, number } = req.body;
// Check if the user with the given email already exists
const existingUser = await Registration.findOne({ email });
if (!existingUser) {
// If user doesn't exist, create a new registration and save it
const registrationData = new Registration({
name,
email,
password,
number,
});
await registrationData.save();
console.log("Regstration successful");
return res.redirect("/success");
} else {
// If user already exists, redirect to an error page
console.log("User already exists");
return res.redirect("/error");
}
} catch (error) {
// Handle any errors during registration
console.log(error);
return res.redirect("/error");
}
});
app.post("/login", async (req, res) => {
try {
const { email, password } = req.body;
// Check if the user with the given email and password exists
const existingUser = await Registration.findOne({ email, password });
if (existingUser) {
// If user exists, redirect to success page (you can customize this)
return res.redirect("/");
} else {
// If user doesn't exist or password is incorrect, redirect to error page
console.log("Invalid email or password");
return res.redirect("/error");
}
} catch (error) {
// Handle any errors during login
console.log(error);
return res.redirect("/error");
}
});
// Success page route
app.get("/success", (req, res) => {
res.sendFile(__dirname + "/pages/success.html");
});
// Resume page route
app.get("/resume", (req, res) => {
res.sendFile(__dirname + "/resume.html");
});
// Error page route
app.get("/error", (req, res) => {
res.sendFile(__dirname + "/pages/error.html");
});
// Route to serve the main CSS file
app.get("/all-styles", (req, res) => {
res.sendFile(__dirname + "/public/css/main.css");
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Server is running http://localhost:${port}`);
});