-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver code
More file actions
177 lines (157 loc) · 4.79 KB
/
server code
File metadata and controls
177 lines (157 loc) · 4.79 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
169
170
171
172
173
174
175
176
177
const express = require('express');
const mysql = require('mysql');
const path = require('path'); // Add this line to import the path module
const app = express();
const port = 5500;
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'rakshan',
database: 'car_rental'
});
// Attempt to connect to the MySQL database
db.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database:', err);
return;
}
console.log('Connected to MySQL database');
});
// Middleware to parse URL-encoded data
app.use(express.urlencoded({ extended: true }));
// Middleware to parse JSON bodies
app.use(express.json());
// Route to handle requests to the root URL
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.get('/rentalsignup-form.html', (req, res) => {
res.sendFile(path.join(__dirname,'public', 'rentalsignup-form.html'));
});
app.get('/carrental.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public','carrental.html'));
});
app.post('/save_data', (req, res) => {
const { username, firstName, lastName, password, otp } = req.body;
const newUser = {
username,
first_name: firstName,
last_name: lastName,
password,
otp
};
// Insert the new user data into the 'users' table
db.query('INSERT INTO users SET ?', newUser, (err, result) => {
if (err) {
console.error('Error inserting data into database:', err);
return res.status(500).json({ error: 'Error inserting data into database' });
}
console.log('User registered successfully');
const htmlContent = `
<script>
alert('User registered successfully!');
window.location.href = '/carrental.html'; // Redirect to carrental.html
</script>
`;
res.status(200).send(htmlContent);
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
app.get('/loginpage.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'loginpage.html'));
});
// Route to serve the car rental page
app.get('/carrental.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'carrental.html'));
});
// Route to handle login form submission
app.post('/login', (req, res) => {
const { username, password } = req.body;
// Query the database for the user with the provided username
db.query('SELECT * FROM users WHERE username = ?', [username], (err, results) => {
if (err) {
console.error('Error querying database:', err);
return res.status(500).json({ error: 'Error querying database' });
}
// If no user found with the provided username, authentication fails
if (results.length === 0) {
return res.status(401).json({ error: 'Invalid username or password' });
}
const user = results[0];
// Compare the provided password with the hashed password stored in the database
if (password !== user.password) {
return res.status(401).json({ error: 'Invalid username or password' });
}
const htmlContent2 = `
<script>
alert('User Login successfully!');
window.location.href = '/carrental.html'; // Redirect to carrental.html
res.status(200).json({ success: true });
</script>
`;
res.redirect('/carrental.html');
});
});
app.post('/booking', (req, res) => {
const { vehicle, pickupLocation, destination, startDate, endDate } = req.body;
let CarID;
switch (vehicle) {
case 'mustang':
CarID = 1;
break;
case 'swift':
CarID = 2;
break;
case 'creta':
CarID = 3;
break;
case 'nexon':
CarID = 4;
break;
case 'scorpio':
CarID = 5;
break;
case 'city':
CarID = 6;
break;
case 'fortuner':
CarID = 7;
break;
case 'seltos':
CarID = 8;
break;
case 'duster':
CarID = 9;
break;
case 'octavia':
CarID = 10;
break;
default:
return res.status(400).json({ error: 'Invalid vehicle selection' });
}
// Create a new booking object
const booking = {
// CarID, // Assign CarID here based on the selected vehicle
// PickupLocation: pickupLocation,
// Destination: destination,
// StartDate: startDate,
// EndDate: endDate,
// };
CarID: 1,
PickupLocation: 'dehradun',
Destination: 'delhi',
StartDate: '2024-04-20',
EndDate: '2024-04-25'
};
// Insert the new booking data into the 'Booking' table
db.query('INSERT INTO Booking SET ?', booking, (err, result) => {
if (err) {
console.error('Error inserting data into database:', err);
return res.status(500).json({ error: 'Error inserting data into database' });
}
console.log('Booking saved successfully');
res.status(200).json({ success: true });
});
});