Bus Booking html
[Link]
<!DOCTYPE html>
<html>
<head><title>
Bus Booking
</title></head>
<body>
<header>
<h1>Bus Booking Site</h1>
</header>
<main>
<form class="busBookingForm" action="/book-bus" method="POST">
<label for = "name">Name : </label>
<input type = "text" id="name" placeholder="Enter your name " name = "name"
required>
<label for = "email">Email : </label>
<input type = "email" id="email" placeholder = "Enter your email" name = "email"
required>
<label for = "journeyDate">Journey Date : </label>
<input type = "date" id="journeyDate" name="journeyDate" required>
<label for = "destination">Destination : </label>
<input type ="text" id ="destination" placeholder="Enter your destination"
name="destination">
<button type = "submit">Book now</button>
</form>
</main>
</body>
<script>
[Link]('form').addEventListener('submit', async (e) => {
[Link](); // Prevent default form submission
const formData = {
name: [Link]('name').value,
email: [Link]('email').value,
journeyDate: [Link]('journeyDate').value,
destination: [Link]('destination').value,
};
try {
const response = await fetch('/book-bus', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: [Link](formData),
});
// Log the response for debugging
[Link]('Server Response:', response);
if ([Link]) {
const message = await [Link]();
[Link]('Message from server:', message); // Should be "Booking successful"
// Redirect to confirmation page
[Link] = '/[Link]';
} else {
const errorText = await [Link]();
[Link]('Error response from server:', errorText);
alert(`Error booking ticket: ${errorText}`);
} catch (error) {
[Link]('Error during booking:', error);
alert('Error booking ticket. Please check the console for details.');
});
</script>
</html>
[Link]
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
// MongoDB connection
[Link]('mongodb://localhost:27017/ticketBooking', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const bookingSchema = new [Link]({
name: { type: String, required: true },
email: { type: String, required: true },
journeyDate: { type: String, required: true },
destination: { type: String, required: true },
});
const Booking = [Link]('Booking', bookingSchema);
// Middleware
[Link]([Link]());
[Link]([Link]({ extended: true })); // For form submissions
// Serve static files from the root directory
[Link]([Link]([Link](__dirname)));
// Routes
[Link]('/book-bus', async (req, res) => {
try {
const bookingData = [Link];
// Debug the incoming data
[Link]('Received booking data:', bookingData);
const booking = new Booking(bookingData);
await [Link]();
[Link](200).send('Booking successful');
} catch (err) {
[Link]('Error saving booking:', err);
[Link](500).send('Error booking ticket');
});
[Link]('connected', () => {
[Link]('Connected to MongoDB');
});
[Link]('error', (err) => {
[Link]('Error connecting to MongoDB:', err);
});
// Start server
[Link](port, () => {
[Link](`Server running at [Link]
});