Skip to content
Related Articles

Related Articles

Hotel Booking System using Node.js and MongoDB

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 03 Mar, 2022

In the hotel booking system, there will be a user for him/her name, email, and room no. they will get after booking. for that, we have to make schema, and as well as we have two APIs. One API for getting data from the database and another API sending data to the database room no, name, email, and all. 


Project setup and Module Installation:

Step 1: Go to your folder where you want to create the API and open it in your IDE and also cmd or PowerShell and run:

npm init -y 

Step 2: Create a file named index.js using the following command:

touch index.js

Step 3: Now Install the mongoose and MongoDB module using the following command:

npm i express mongoose mongodb cors

 Project Structure: It will look like this. 

Project directory

Example: Now write down the following code in the index.js file


// To connect with your mongoDB database
const mongoose = require('mongoose');
    dbName: 'yourDB-name',
    useNewUrlParser: true,
    useUnifiedTopology: true,
  (err) => (err ? console.log(err) : 
    console.log('Connected to yourDB-name database')),
// Schema for hotel Booking
const UserSchema = new mongoose.Schema({
  name: {
    type: String,
  email: {
    type: String,
    required: true,
    unique: true,
  roomNo: {
    type: String,
    required: true,
  date: {
    type: Date,
const RoomBooked = mongoose.model('users', UserSchema);
// For backend and express
const express = require('express');
const cors = require('cors');
const app = express();
app.get('/', (req, resp) => {
  resp.send('App is Working');
// Register data to book hotelroom'/register', async (req, resp) => {
  try {
    const user = new RoomBooked(req.body);
    let result = await;
    result = result.toObject();
    if (result) {
      delete result.password;
    } else {
      console.log('User already register');
  } catch (e) {
    resp.send('Something Went Wrong');
// Getting roombooked details
app.get('/get-room-data', async (req, resp) => {
  try {
    const details = await RoomBooked.find({});
  } catch (error) {
// Server setup
app.listen(5000, () => {
  console.log('App listen at port 5000');

Run the application: Run the following command to start the application:

node index.js 

Output: API is created for booking rooms and getting details. 

  • For Register or booking

  • Get booked data from the database


Note: If you open your MongoDB you can see this data within it 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!