-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
344 lines (319 loc) · 14.7 KB
/
server.js
File metadata and controls
344 lines (319 loc) · 14.7 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const db = require('./database'); // Assuming database.js is in the same directory
const multer = require('multer'); // NEW: Import multer
const fs = require('fs'); // NEW: Import file system module for creating directories
const app = express();
const port = 3000;
// --- Multer Configuration for File Uploads (NEW) ---
const UPLOADS_DIR = path.join(__dirname, 'public', 'uploads'); // Directory to store uploaded images
// Create the uploads directory if it doesn't exist
if (!fs.existsSync(UPLOADS_DIR)) {
fs.mkdirSync(UPLOADS_DIR, { recursive: true });
console.log(`Created uploads directory at: ${UPLOADS_DIR}`);
}
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, UPLOADS_DIR); // Files will be saved in the 'public/uploads' directory
},
filename: function (req, file, cb) {
// Create a unique filename: fieldname-timestamp.ext
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
}
});
// Configure multer to accept multiple files for the 'dog_photos' field
// Max 5 files, each up to 5MB
const upload = multer({
storage: storage,
limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB limit per file
fileFilter: (req, file, cb) => {
// Accept only image files
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only image files are allowed!'), false);
}
}
});
// Middleware
// bodyParser.urlencoded is still needed for other forms (like contact form)
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files (HTML, CSS, images, and now uploaded images) from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// --- Routes for serving your HTML pages ---
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'HomePage.html'));
});
app.get('/booking', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'booking.html'));
});
app.get('/aboutus.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'aboutus.html'));
});
app.get('/contactus.html', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'contactus.html'));
});
app.get('/admin-dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin_dashboard.html'));
});
// --- API endpoint to get all bookings ---
app.get('/api/bookings', (req, res) => {
const sql = `SELECT * FROM bookings ORDER BY booking_date DESC`;
db.all(sql, [], (err, rows) => {
if (err) {
console.error('Error fetching bookings:', err.message);
return res.status(500).json({ error: err.message });
}
res.json(rows);
});
});
// --- API endpoint to get all contact messages ---
app.get('/api/contact-messages', (req, res) => {
const sql = `SELECT * FROM contact_messages ORDER BY submission_date DESC`;
db.all(sql, [], (err, rows) => {
if (err) {
console.error('Error fetching contact messages:', err.message);
return res.status(500).json({ error: err.message });
}
res.json(rows);
});
});
// --- Route to handle booking submissions (MODIFIED for Multer) ---
// Use upload.array('dog_photos', 5) middleware to handle file uploads
app.post('/submit_booking', upload.array('dog_photos', 5), (req, res) => {
// req.body will now contain text fields, and req.files will contain file info
const { name, email, start_datetime, end_datetime, location, notes, total_cost } = req.body;
const uploadedFiles = req.files; // Array of uploaded file objects
// Extract paths of uploaded files
const photoPaths = uploadedFiles ? uploadedFiles.map(file => `/uploads/${file.filename}`) : [];
const photos_json = JSON.stringify(photoPaths); // Store as JSON string in DB
// --- Process Multiple Dog Data from form fields ---
const dogs = [];
let i = 0;
while (req.body[`dog_breed_${i}`] !== undefined && req.body[`dog_gender_${i}`] !== undefined) {
dogs.push({
breed: req.body[`dog_breed_${i}`],
gender: req.body[`dog_gender_${i}`]
});
i++;
}
const num_dogs = dogs.length;
const dogs_data_json = JSON.stringify(dogs);
// Insert data into the database
db.run(
`INSERT INTO bookings (name, email, num_dogs, start_datetime, end_datetime, location, notes, dogs_data_json, total_cost, photos_json)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, // Added one more '?' for photos_json
[name, email, num_dogs, start_datetime, end_datetime, location, notes, dogs_data_json, total_cost, photos_json], // Added photos_json here
function (err) {
if (err) {
console.error('Error inserting booking:', err.message);
// If there's a DB error, try to clean up uploaded files
if (uploadedFiles) {
uploadedFiles.forEach(file => {
fs.unlink(file.path, (unlinkErr) => {
if (unlinkErr) console.error('Error deleting uploaded file:', unlinkErr);
});
});
}
res.status(500).send('Error submitting booking request. Please try again.');
return;
}
console.log(`A booking has been inserted with rowid ${this.lastID}`);
// --- Detailed HTML confirmation response ---
let dogsHtml = '';
if (dogs.length > 0) {
dogsHtml += '<p><strong>Dog(s) Details:</strong></p><ul>';
dogs.forEach((dog, index) => {
dogsHtml += `<li>Dog ${index + 1}: ${dog.breed} (${dog.gender})</li>`;
});
dogsHtml += '</ul>';
} else {
dogsHtml += '<p>No specific dog details provided.</p>';
}
let photosConfirmationHtml = '';
if (photoPaths.length > 0) {
photosConfirmationHtml += '<p><strong>Uploaded Photos:</strong></p><div style="display: flex; flex-wrap: wrap; gap: 10px; justify-content: center;">';
photoPaths.forEach(path => {
photosConfirmationHtml += `<img src="${path}" alt="Dog Photo" style="width: 80px; height: 80px; object-fit: cover; border-radius: 5px;">`;
});
photosConfirmationHtml += '</div>';
} else {
photosConfirmationHtml += '<p>No photos uploaded.</p>';
}
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Booking Confirmed</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="logologo.jpeg" type="image/jpeg">
<style>
/* Inline styles for confirmation page (can be moved to style.css) */
body {
background-image: linear-gradient(to right, #fffdd0 , rgb(255, 213, 128));
font-family: sans-serif;
margin: 0;
padding-top: 60px;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.confirmation-container {
width: 80%;
max-width: 600px;
padding: 30px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 50px;
text-align: left;
}
.confirmation-container h2 {
color: #f19d30;
text-align: center;
margin-bottom: 20px;
}
.confirmation-container p {
color: #333;
line-height: 1.6;
margin-bottom: 10px;
}
.confirmation-container ul {
list-style-type: disc;
margin-left: 20px;
margin-bottom: 10px;
}
.confirmation-container ul li {
margin-bottom: 5px;
color: #555;
}
.confirmation-container strong {
color: #333;
}
.back-button {
display: inline-block;
padding: 10px 20px;
background-color: #f19d30;
color: white;
text-decoration: none;
border-radius: 5px;
margin-top: 20px;
transition: background-color 0.3s ease;
}
.back-button:hover {
background-color: #e08e2b;
}
</style>
</head>
<body>
<main class="confirmation-container">
<h2>Booking Request Received!</h2>
<p>Thank you, <strong>${name}</strong>, for your booking request. We will contact you soon at <strong>${email}</strong>.</p>
<p>Your dog sitting is scheduled from <strong>${new Date(start_datetime).toLocaleString()}</strong> to <strong>${new Date(end_datetime).toLocaleString()}</strong>.</p>
<p>Location: <strong>${location}</strong></p>
${notes ? `<p>Special Instructions: <strong>${notes}</strong></p>` : ''}
<p>Total number of dogs: <strong>${num_dogs}</strong></p>
${dogsHtml}
<p><strong>Estimated Total Cost: Rs. ${total_cost}</strong></p>
${photosConfirmationHtml} <!-- NEW: Display uploaded photos -->
<a href="/" class="back-button">Back to Homepage</a>
</main>
</body>
</html>
`);
}
);
});
// --- Route to handle contact form submissions ---
app.post('/submit_contact', (req, res) => {
const { name, email, subject, message } = req.body;
db.run(
`INSERT INTO contact_messages (name, email, subject, message) VALUES (?, ?, ?, ?)`,
[name, email, subject, message],
function (err) {
if (err) {
console.error('Error inserting contact:', err.message);
res.status(500).send('Error submitting contact form. Please try again.');
return;
}
console.log(`A contact message has been inserted with rowid ${this.lastID}`);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Contact Message Sent</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="logologo.jpeg" type="image/jpeg">
<style>
body {
background-image: linear-gradient(to right, #fffdd0 , rgb(255, 213, 128));
font-family: sans-serif;
margin: 0;
padding-top: 60px;
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.confirmation-container {
width: 80%;
max-width: 600px;
padding: 30px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 50px;
text-align: left;
}
.confirmation-container h2 {
color: #f19d30;
text-align: center;
margin-bottom: 20px;
}
.confirmation-container p {
color: #333;
line-height: 1.6;
margin-bottom: 10px;
}
.confirmation-container strong {
color: #333;
}
.back-button {
display: inline-block;
padding: 10px 20px;
background-color: #f19d30;
color: white;
text-decoration: none;
border-radius: 5px;
margin-top: 20px;
transition: background-color 0.3s ease;
}
.back-button:hover {
background-color: #e08e2b;
}
</style>
</head>
<body>
<main class="confirmation-container">
<h2>Thank You for Your Message!</h2>
<p>Dear <strong>${name}</strong>,</p>
<p>We have successfully received your message.</p>
<p><strong>Subject:</strong> ${subject || 'No Subject'}</p>
<p><strong>Your Message:</strong></p>
<p style="white-space: pre-wrap; background-color: #f9f9f9; padding: 15px; border-radius: 5px; border: 1px solid #eee;">${message}</p>
<p>We will get back to you shortly at <strong>${email}</strong>.</p>
<a href="/" class="back-button">Back to Homepage</a>
</main>
</body>
</html>
`);
}
);
});
// Start the server
app.listen(port, () => {
console.log(`Bark Buddies server listening at http://localhost:${port}`);
});