In this article, we'll explore the process of building a restaurant recommendation application using Angular for the client side and Node.js with Express for the server side. This application will allow users to search for restaurants based on location, cuisine, and rating, providing them with a curated list of dining options.
Client-side: The client-side will be built using Angular, a popular JavaScript framework for building web applications. We'll create the necessary components, services, and routing to handle the user interface and interact with the server-side.
Server-side: The server-side will be built using Node.js and Express, a web application framework for Node.js. We'll create the API endpoints, handle database operations using Mongoose, and manage Cross-Origin Resource Sharing (CORS) policies.
Steps to Create the Project
Step 1: Create a new directory for your server-side project:
mkdir server cd server
Step 2: Initialize a new Node.js project
npm init -y
Step 3: Install the required dependencies
npm install body-parser cores cors dotenv express mongoose --save
Project Structure
Backend Folder StructureUpdated dependencies will look like:
create restaurant.controller.js inside the component folder
create Restaurant.model.js inside models folder
create restaurant.routes.js insider route folder
JavaScript
// controllers/restaurant.controller.jsconstRestaurantModel=require("../models/restaurant.model");constgetRestaurantsByFilters=async(req,res)=>{console.log("request body is",req.body);try{const{location,rating,cuisines}=req.body;constquery={};if(location){query.location=location;}if(rating?.length>0){if(rating==3){query.rating={$gte:3,$lte:5};}else{query.rating={$gte:4,$lte:5};}}if(cuisines?.length>0){query.cuisines={$in:cuisines};}constrestaurants=awaitRestaurantModel.find(query);if(restaurants.length>0){res.json({success:true,data:restaurants});}else{res.json({success:false,message:"No such data found!"});}}catch(error){res.json({success:false,error:error.message});}};module.exports={getRestaurantsByFilters};
//index.jsconstexpress=require("express");constmongoose=require("mongoose");constcors=require("cors");constapp=express();app.use(express.json());app.use(cors());// Connect to MongoDBconstMONGO_URL="Your mongo link";mongoose.connect(MONGO_URL,{useNewUrlParser:true,useUnifiedTopology:true,}).then(()=>{console.log("Connected to MongoDB");seedRestaurantData();startServer();}).catch((error)=>{console.log("Failed to connect to MongoDB",error);});// Define the restaurant modelconstrestaurantSchema=newmongoose.Schema({name:String,address:String,contact:String,location:String,rating:Number,offers:Boolean,cuisines:[String],image:String,});constRestaurantModel=mongoose.model("Restaurant",restaurantSchema);asyncfunctionseedRestaurantData(){awaitRestaurantModel.deleteMany({});constrestaurantData=[{name:"Tandoor Palace",address:"123 Main Street, Hyderabad",contact:"123-456-7890",location:"Hyderabad",rating:3.5,offers:true,cuisines:["North Indian","Biryani"],image:"https://media.geeksforgeeks.org/wp-content/uploads/20240110004646/file.jpg",},{name:"Sushi Samba",address:"456 Oak Avenue, Bangalore",contact:"987-654-3210",location:"Bangalore",rating:4.8,offers:true,cuisines:["Japanese","Sushi"],image:"https://media.geeksforgeeks.org/wp-content/uploads/20240110004646/file.jpg",},{name:"La Trattoria",address:"789 Elm Street, Mumbai",contact:"456-789-0123",location:"Mumbai",rating:3.2,offers:false,cuisines:["Italian","Pastas","Pizzas"],image:"https://media.geeksforgeeks.org/wp-content/uploads/"+"20240110004602/pexels-chan-walrus-958545-(1).jpg",},{name:"Spice Garden",address:"321 Oak Road, Delhi",contact:"789-012-3456",location:"Delhi",rating:2.7,offers:true,cuisines:["South Indian","Vegetarian"],image:"https://media.geeksforgeeks.org/wp-content/uploads/20240110004646/file.jpg",},{name:"Chopsticks",address:"654 Maple Lane, Pune",contact:"012-345-6789",location:"Pune",rating:4.3,offers:true,cuisines:["Chinese","Asian Fusion"],image:"https://media.geeksforgeeks.org/wp-content/uploads/"+"20240110004602/pexels-chan-walrus-958545-(1).jpg",},{name:"Seaside Grill",address:"987 Beach Road, Chennai",contact:"345-678-9012",location:"Chennai",rating:3.6,offers:true,cuisines:["Seafood","Barbecue"],image:"https://media.geeksforgeeks.org/wp-content/uploads/20240110004646/file.jpg",},{name:"Pasta Perfetto",address:"159 Elm Avenue, Hyderabad",contact:"678-901-2345",location:"Hyderabad",rating:2.4,offers:false,cuisines:["Italian","Pastas"],image:"https://media.geeksforgeeks.org/wp-content/uploads/"+"20240110004602/pexels-chan-walrus-958545-(1).jpg",},{name:"Sizzle Steak House",address:"753 Oak Street, Bangalore",contact:"901-234-5678",location:"Bangalore",rating:3.9,offers:true,cuisines:["Steak","American"],image:"https://media.geeksforgeeks.org/wp-content/uploads/20240110004646/file.jpg",},{name:"Dosa Delights",address:"369 Main Road, Chennai",contact:"234-567-8901",location:"Chennai",rating:4.1,offers:true,cuisines:["South Indian","Vegetarian"],image:"https://media.geeksforgeeks.org/wp-content/uploads/"+"20240110004602/pexels-chan-walrus-958545-(1).jpg",},{name:"Bella Napoli",address:"852 Pine Avenue, Mumbai",contact:"567-890-1234",location:"Mumbai",rating:4.6,offers:true,cuisines:["Italian","Pizzas"],image:"https://media.geeksforgeeks.org/wp-content/uploads/"+"20240110004602/pexels-chan-walrus-958545-(1).jpg",},];awaitRestaurantModel.insertMany(restaurantData);console.log("Restaurant data seeded successfully!");}functionstartServer(){app.get("/health",(req,res)=>{res.status(200).json("Server is up and running");});app.get("/api/restaurants",async(req,res)=>{try{constrestaurants=awaitRestaurantModel.find({});res.status(200).json({success:true,data:restaurants});}catch(error){res.status(500).json({success:false,error:error.message});}});constPORT=process.env.PORT||4000;app.listen(PORT,()=>{console.log(`Server is running on port ${PORT}`);});}
To start the server run the following command.
node index.js
Step 4: Run the following command to install Angular CLI globally: