Discover how to build an efficient booking platform using Lovable. Our step-by-step guide offers expert tips to design, develop, and launch with ease.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Setting Up Your Lovable Project
Configuring Dependencies in Lovable
lovable.config.js
in the root directory of your project.
module.exports = {
dependencies: {
"express": "^4.17.1"
// Include any additional dependencies here
}
};
Creating the Backend Server
server.js
.
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse JSON and URL-encoded data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Endpoint to retrieve bookings
app.get('/bookings', (req, res) => {
// In a real app, retrieve bookings from your database
res.json({ message: 'List of bookings will appear here.' });
});
// Endpoint to create a new booking
app.post('/bookings', (req, res) => {
// In a real app, save booking data to your database and perform data validation
console.log(req.body);
res.json({ message: 'Booking created successfully.' });
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Defining the Application Start Command
lovable.json
in the root directory. This configuration file tells Lovable how to start your app.
{
"start": "node server.js",
"env": {
"PORT": "3000"
}
}
Creating the Frontend Booking Page
index.html
in the root directory. This file will serve as the main page for users to create bookings.index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Booking Platform</title>
</head>
<body>
<h1>Book Your Experience</h1>
<form id="bookingForm">
<input type="text" name="name" placeholder="Your Name" required />
<input type="email" name="email" placeholder="Your Email" required />
<input type="date" name="date" required />
<button type="submit">Book Now</button>
</form>
<script src="booking.js"></script>
</body>
</html>
Creating the Frontend Script
booking.js
in the root directory. This script will handle the form submission.booking.js
:
document.getElementById('bookingForm').addEventListener('submit', async (e) => {
e.preventDefault();
// Gather form data
const formData = new FormData(e.target);
const data = {};
formData.forEach((value, key) => {
data[key] = value;
});
try {
// Send form data to the backend /bookings endpoint
const response = await fetch('/bookings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
const result = await response.json();
alert(result.message);
} catch (error) {
alert('Error creating booking');
}
});
Testing Your Booking Platform Locally
server.js
using the configuration in lovable.json
.index.html
.
Deploying Your Booking Platform
lovable.json
if necessary for production environments.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// In-memory data structure for Lovable booking platform
const bookings = [];
// POST /api/bookings - Create a new booking for a Lovable experience
app.post('/api/bookings', (req, res) => {
const { userId, experienceId, date, paymentInfo } = req.body;
if (!userId || !experienceId || !date || !paymentInfo) {
return res.status(400).json({ error: 'Missing required fields' });
}
// Create a new booking entry with structured data
const booking = {
bookingId: bookings.length + 1,
userId,
experienceId,
date,
status: 'pending',
paymentInfo // This may include card details, transaction ids etc.
};
bookings.push(booking);
res.status(201).json({ booking });
});
// PUT /api/bookings/:id/status - Update booking status (confirm or cancel)
app.put('/api/bookings/:id/status', (req, res) => {
const bookingId = parseInt(req.params.id, 10);
const { status } = req.body;
const booking = bookings.find(b => b.bookingId === bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}
if (!['confirmed', 'cancelled'].includes(status)) {
return res.status(400).json({ error: 'Invalid status value' });
}
booking.status = status;
res.json({ booking });
});
// GET /api/bookings/:id - Retrieve details of a specific booking
app.get('/api/bookings/:id', (req, res) => {
const bookingId = parseInt(req.params.id, 10);
const booking = bookings.find(b => b.bookingId === bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}
res.json({ booking });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Lovable Booking API server running on port ${PORT}`);
});
const express = require('express');
const axios = require('axios');
const router = express.Router();
// In-memory store for bookings
const bookings = [];
// POST /api/bookings/pay - Process booking and handle payment via an external API
router.post('/api/bookings/pay', async (req, res) => {
const { userId, experienceId, date, paymentToken } = req.body;
if (!userId || !experienceId || !date || !paymentToken) {
return res.status(400).json({ error: 'Missing required fields' });
}
try {
// Call the external payment API to charge the user
const paymentResponse = await axios.post('https://api.externalpayments.com/charge', {
token: paymentToken,
amount: 2000, // Example fixed amount in cents
currency: 'USD'
});
if (paymentResponse.data.status !== 'succeeded') {
return res.status(402).json({ error: 'Payment processing failed' });
}
// Create a new booking entry after successful payment
const booking = {
bookingId: bookings.length + 1,
userId,
experienceId,
date,
status: 'confirmed',
paymentDetails: paymentResponse.data
};
bookings.push(booking);
res.status(201).json({ booking });
} catch (error) {
console.error('Payment API error:', error.message);
res.status(500).json({ error: 'Internal server error during payment processing' });
}
});
module.exports = router;
const express = require('express');
const moment = require('moment');
const axios = require('axios');
const app = express();
app.use(express.json());
const bookings = [
// Example booking:
// {
// bookingId: 1,
// userId: 'user123',
// experienceId: 'exp456',
// date: '2023-12-25T15:00:00Z',
// status: 'confirmed',
// paymentInfo: { paymentId: 'pay789', amount: 2500 }
// }
];
app.post('/api/bookings/:id/cancel', async (req, res) => {
const bookingId = parseInt(req.params.id, 10);
const booking = bookings.find(b => b.bookingId === bookingId);
if (!booking) {
return res.status(404).json({ error: 'Booking not found' });
}
if (booking.status !== 'confirmed') {
return res.status(400).json({ error: 'Only confirmed bookings can be cancelled' });
}
const now = moment();
const bookingDate = moment(booking.date);
if (bookingDate.diff(now, 'hours') < 48) {
return res.status(400).json({ error: 'Cancellation period expired. Must cancel at least 48 hours before the booking date.' });
}
try {
const refundResponse = await axios.post('https://api.paymentgateway.com/refund', {
paymentId: booking.paymentInfo.paymentId,
amount: booking.paymentInfo.amount
});
if (refundResponse.data.status !== 'succeeded') {
return res.status(402).json({ error: 'Refund failed, cancellation aborted' });
}
booking.status = 'cancelled';
res.json({ booking, refund: refundResponse.data });
} catch (error) {
res.status(500).json({ error: 'Error processing cancellation and refund', details: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Understanding the Booking Platform and AI Code Generators
Gathering Requirements and Planning the Platform
Choosing the Right AI Code Generator
Setting Up the Development Environment
Developing the Booking Platform Backend
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse JSON bodies
app.use(express.json());
// Sample route for booking
app.post('/api/book', (req, res) => {
// Your booking logic goes here
res.status(200).json({ message: 'Booking received!' });
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Integrating AI Code Generation into Your Workflow
Building the Front-End Interface
Testing and Debugging
Deploying the Booking Platform
Monitoring and Maintaining the Platform
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.