Building a Restaurant Reservation System with Node.js, Express & MongoDB - A Beginner's Guide
Tags: #nodejs #express #mongodb #webdevelopment #tutorial #beginner
Introduction
Hey everyone! 👋 This is my first Dev.to post, and I'm excited to share what I've been learning. As a 5th-semester CS student, I've been diving deep into full-stack web development, and today I want to walk you through building a Restaurant Reservation System – a real project I built that taught me so much about backend architecture and database design.
If you're just starting with Node.js, Express, and MongoDB, this post is for you!
What We'll Build
A simple but functional restaurant reservation system where:
- Users can browse available time slots
- Users can book a table for a specific date and time
- Admin can manage reservations
- Weekly scheduling (Monday-Sunday)
- 2-hour time slots
Tech Stack:
- Backend: Node.js + Express
- Database: MongoDB
- Frontend: React + Tailwind CSS (we'll focus on backend in this post)
Prerequisites
Before we start, make sure you have:
- Node.js installed
- MongoDB running locally or MongoDB Atlas account
- Basic JavaScript knowledge
- VS Code or any code editor
Project Setup
1. Initialize the Project
mkdir restaurant-reservation-system
cd restaurant-reservation-system
npm init -y
2. Install Dependencies
npm install express mongoose cors dotenv
npm install nodemon --save-dev
3. Create Project Structure
restaurant-reservation-system/
├── models/
│ └── Reservation.js
├── routes/
│ └── reservations.js
├── config/
│ └── db.js
├── .env
├── server.js
└── package.json
Step 1: Set Up MongoDB Connection
config/db.js
const mongoose = require('mongoose');
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI);
console.log('MongoDB connected successfully');
} catch (error) {
console.log('MongoDB connection failed:', error);
process.exit(1);
}
};
module.exports = connectDB;
Step 2: Create Reservation Model
models/Reservation.js
const mongoose = require('mongoose');
const ReservationSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
phone: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
},
time: {
type: String,
required: true,
},
guests: {
type: Number,
required: true,
},
status: {
type: String,
enum: ['pending', 'confirmed', 'cancelled'],
default: 'pending',
},
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('Reservation', ReservationSchema);
Step 3: Create API Routes
routes/reservations.js
const express = require('express');
const router = express.Router();
const Reservation = require('../models/Reservation');
// Get all reservations
router.get('/', async (req, res) => {
try {
const reservations = await Reservation.find();
res.json(reservations);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Create a new reservation
router.post('/', async (req, res) => {
const reservation = new Reservation({
name: req.body.name,
email: req.body.email,
phone: req.body.phone,
date: req.body.date,
time: req.body.time,
guests: req.body.guests,
});
try {
const newReservation = await reservation.save();
res.status(201).json(newReservation);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Update reservation status
router.patch('/:id', async (req, res) => {
try {
const reservation = await Reservation.findById(req.params.id);
if (req.body.status) {
reservation.status = req.body.status;
}
const updatedReservation = await reservation.save();
res.json(updatedReservation);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Delete reservation
router.delete('/:id', async (req, res) => {
try {
await Reservation.findByIdAndDelete(req.params.id);
res.json({ message: 'Reservation deleted' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
module.exports = router;
Step 4: Set Up Express Server
server.js
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const connectDB = require('./config/db');
const app = express();
// Connect to MongoDB
connectDB();
// Middleware
app.use(cors());
app.use(express.json());
// Routes
const reservationRoutes = require('./routes/reservations');
app.use('/api/reservations', reservationRoutes);
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
.env
MONGODB_URI=mongodb+srv://yourUsername:yourPassword@cluster.mongodb.net/restaurant
PORT=5000
Step 5: Run Your Project
Add this to package.json scripts:
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
}
Then run:
npm run dev
Testing the API
Use Postman or curl to test:
Create a reservation:
POST http://localhost:5000/api/reservations
{
"name": "John Doe",
"email": "john@example.com",
"phone": "03001234567",
"date": "2026-08-20",
"time": "19:00",
"guests": 4
}
Get all reservations:
GET http://localhost:5000/api/reservations
Key Learnings
During this project, I learned:
- Database Design - How to structure data for real-world applications
- RESTful APIs - Creating clean, maintainable endpoints
- Error Handling - Proper validation and error responses
- Middleware - Using Express middleware for parsing and CORS
- Async/Await - Managing asynchronous operations cleanly
What's Next?
You can extend this project by:
- Adding authentication (JWT)
- Implementing time slot availability logic
- Adding email notifications
- Creating a frontend with React
- Adding payment integration
- Deploying to production
Conclusion
Building this restaurant reservation system taught me that the best way to learn web development is by building real projects. Start simple, then gradually add complexity.
If you're just starting your web development journey, I highly recommend building this project yourself. Don't just copy-paste – try to understand each part and modify it.
Questions? Drop them in the comments! I'd love to help. Also, follow me on GitHub for more projects!
Happy coding! 🚀
Resources:
About Me
I'm a 5th-semester CS student at Sukkur IBA University learning full-stack web development. I believe in learning by building real projects. You can find all my projects on GitHub.
Top comments (0)