-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (126 loc) · 4.52 KB
/
Copy pathserver.js
File metadata and controls
160 lines (126 loc) · 4.52 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
// Require the necessary modules
const express = require('express'); // Express web framework
const mongoose = require('mongoose'); // MongoDB ODM
const MongoClient = require('mongodb').MongoClient; // MongoDB driver
// Create a new Express application
const app = express();
// Serve static files from the public directory
app.use(express.static('public'));
// Set up a route for the login page
app.get('/index.html', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
function isAuthenticated(req, res, next) {
if (req.session && req.session.isAuthenticated) {
return next();
}
res.redirect('/index.html');
}
// Route to serve Bootstrap CSS from node_modules directory
app.get('/node_modules/bootstrap/dist/css/bootstrap.min.css', function (req, res) {
res.setHeader('Content-Type', 'text/css');
res.sendFile(__dirname + '/node_modules/bootstrap/dist/css/bootstrap.min.css');
});
// Connect to MongoDB database using a connection string
mongoose.connect('mongodb+srv://geekabhinav007:Abhinav2004825@mobilewebsite.578pvnr.mongodb.net/?retryWrites=true&w=majority', { useNewUrlParser: true })
.then(() => console.log('MongoDB connected')) // Log success message to console
.catch(err => console.log(err)); // Log error message to console if connection fails
// Define a schema for the contact form data
const contactSchema = new mongoose.Schema({
name: String,
email: String,
message: String
});
// Define a schema for the user data
const userSchema = new mongoose.Schema({
username: String,
password: String,
});
const User = mongoose.model('User', userSchema);
// Create Mongoose models for the contact and user schemas
const Contact = mongoose.model('Contact', contactSchema);
// Use the Express JSON parser middleware to parse incoming JSON data
app.use(express.json());
// Handle registration form submissions
app.post('/register', async (req, res) => {
const { username, password, confirmPassword } = req.body;
// Check if the passwords match
if (password !== confirmPassword) {
return res.status(400).send('Passwords do not match');
}
// Check if the user already exists in the database
const existingUser = await User.findOne({ username });
if (existingUser) {
return res.status(400).send('User already exists');
}
// Create a new user object using the User model
const newUser = new User({ username, password });
try {
// Save the new user object to the database
await newUser.save();
console.log('Registration successful');
res.status(200).send('Registration successful');
} catch (error) {
console.error(error);
res.status(500).send('An error occurred');
}
});
// Handle login form submissions
app.post('/index', async (req, res) => {
const { username, password } = req.body;
try {
// Check if the user exists in the database
const user = await User.findOne({ username });
if (!user) {
return res.status(400).send('User does not exist');
}
// Check if the password matches
if (user.password !== password) {
return res.status(400).send('Incorrect password');
}
// If both username and password are correct, redirect to the home.html page
res.redirect('/index.html');
} catch (error) {
console.error(error);
res.status(500).send('An error occurred');
}
});
// Handle logout
app.get('/logout', (req, res) => {
req.session.destroy(err => {
if (err) {
console.error(err);
res.status(500).send('An error occurred');
} else {
res.redirect('/index.html');
}
});
});
// Route to handle form submissions
app.post('/submit', async (req, res) => {
const { name, email, message } = req.body;
// Create a new contact object using the Contact model
const newContact = new Contact({ name, email, message });
try {
// Save the new contact object to the database
await newContact.save();
res.send('Form submitted successfully');
console.log('Form submitted successfully');
} catch (error) {
console.error(error);
res.status(500).send('An error occurred');
}
});
// Set up a route for the login page
app.get('/', (req, res) => {
res.sendFile(__dirname + 'index.html');
});
// Set up a route for the index page
app.get('/index.html', isAuthenticated, (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Start the server and listen for incoming connections
const port = process.env.PORT || 4000; // Use the value of the PORT environment variable, or 4000 if it is not set
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});