Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to Get Data from MongoDB using Node.js?
Next article icon

Hotel Booking System using Node.js and MongoDB

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

Prerequisite:

  • NodeJS installed in your system (install)
  • MongoDB installed in your system (install)
  • Postman desktop or Thunderclient VScode extension (install)

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

index.j
// To connect with your mongoDB database const mongoose = require('mongoose');  mongoose.connect(   'mongodb://localhost:27017/',   {     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,     default: Date.now,   }, });  const RoomBooked = mongoose.model('users', UserSchema); RoomBooked.createIndexes();  // For backend and express const express = require('express'); const cors = require('cors');  const app = express(); app.use(express.json()); app.use(cors());  app.get('/', (req, resp) => {   resp.send('App is Working'); });  // Register data to book hotelroom app.post('/register', async (req, resp) => {   try {     const user = new RoomBooked(req.body);     let result = await user.save();     result = result.toObject();     if (result) {       delete result.password;       resp.send(req.body);       console.log(result);     } 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({});     resp.send(details);   } catch (error) {     console.log(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

http://localhost:5000/register

Get booked data from the database

http://localhost:5000/get-room-data
Note: If you open your MongoDB you can see this data within it 

Next Article
How to Get Data from MongoDB using Node.js?
author
krcpr007
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • System-Design
  • NodeJS-Questions

Similar Reads

  • How to Get Data from MongoDB using Node.js?
    One can create a simple Node.js application that allows us to get data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. Also, we use the EJS for our front end to render the simple HTML form and a table to show the data. Prerequisi
    6 min read
  • Signup Form Using Node.js and MongoDB
    Installations First, we need to include a few packages for our Nodejs application. npm install express --save Express allows us to set up middlewares to respond to HTTP Requests. npm install body-parser --save If you want to read HTTP POST data , you have to use the "body-parser" node module. npm in
    3 min read
  • Login form Using NodeJS and MongoDB
    Follow these simple steps to learn how to create a login form using NodeJS and MongoDB. NodeJS login form allows users to log in to the website after they have created their account using the signup form. We will be using the following technologies: NodeJS & Express – Backend server and routingM
    4 min read
  • How to build Hostel Management System using Node.js ?
    In this article, we are going to create a Hostel Management System. A Hostel Management System is used to manage the record of students of a college to which the college provides a hostel, where a college can view all the student data including their names, roll number, date of birth, city, phone nu
    9 min read
  • How to Create Indexes in MongoDB using Node.js?
    MongoDB, a popular NoSQL database, provides powerful indexing capabilities to improve query performance. Indexes in MongoDB help in quickly locating documents and speeding up read operations. In this tutorial, we'll explore how to create indexes in MongoDB using Node.js. What is an Index in MongoDB?
    3 min read
  • How to drop database of MongoDB using Node.js ?
    MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
    2 min read
  • How to Connect to a MongoDB Database Using Node.js
    MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
    4 min read
  • How to Build Hospital Management System using Node.js?
    In this article, we are going to create a Hospital Management System. A Hospital Management System is basically used to manage patients in the hospital. It is helpful to see which patients do not have a bed allotted or if there are any free beds or not. It makes sure that the discharged patients' be
    7 min read
  • How to Perform a findOne Operation in MongoDB using Node.js?
    The findOne operation in MongoDB is used to get a single document from the collection if the given query matches the collection record. While using findOne, if more than one record is there with the exact same match, then it will return the very first one. We will use this operation if we need to fe
    4 min read
  • Building an OTP Verification System with Node.js and MongoDB
    In the present digital world, Securing your website or internet could be very crucial. One manner to increase protection is by using One Time Password (OTP) for the verification system. This will help you to steady your software and defend your website from unauthorized get entry. With increasing co
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences