├── .gitignore ├── vercel.json ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env 3 | .vercel 4 | . gitngore 5 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "./index.js", 6 | "use": "@vercel/node" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src": "/(.*)", 12 | "dest": "/", 13 | "methods": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] 14 | } 15 | ] 16 | } 17 | 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server-side", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "npm index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "cors": "^2.8.5", 15 | "dotenv": "^16.4.5", 16 | "express": "^4.19.2", 17 | "mongodb": "^6.5.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // index js 2 | const express = require('express'); 3 | const cors = require('cors'); 4 | require('dotenv').config() 5 | const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb'); 6 | 7 | 8 | const app = express(); 9 | const port = process.env.PORT || 5000; 10 | 11 | // Middleware 12 | app.use(cors()); 13 | app.use(express.json()); 14 | 15 | // MongoDB Database 16 | const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ahe248t.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`; 17 | 18 | // Create a MongoClient with a MongoClientOptions object to set the Stable API version 19 | const client = new MongoClient(uri, { 20 | serverApi: { 21 | version: ServerApiVersion.v1, 22 | strict: true, 23 | deprecationErrors: true, 24 | } 25 | }); 26 | 27 | async function run() { 28 | try { 29 | // Connect the client to the server (optional starting in v4.7) 30 | // await client.connect(); 31 | 32 | const db = client.db("TouristDB"); 33 | const countriesCollection = db.collection('countries'); 34 | const touristSpotsCollection = db.collection('touristSpots'); 35 | 36 | app.get('/tourist-spots', async (req, res) => { 37 | const cursor = touristSpotsCollection.find(); 38 | const result = await cursor.toArray(); 39 | res.send(result); 40 | }) 41 | 42 | app.get('/tourist-spots/:id', async (req, res) => { 43 | const id = req.params.id; 44 | const query = { _id: new ObjectId(id) }; 45 | const result = await touristSpotsCollection.findOne(query); 46 | res.send(result); 47 | }) 48 | 49 | app.get('/tourist-spots-sort/', async (req, res) => { 50 | const cursor = touristSpotsCollection.find().sort({average_cost: 1}).collation({locale: "en_US", numericOrdering: true}); 51 | const result = await cursor.toArray(); 52 | res.send(result); 53 | }) 54 | 55 | app.get('/tourist-spots-user/:email', async (req, res) => { 56 | const userEmail = req.params.email; 57 | const query = { email: userEmail } 58 | const cursor = touristSpotsCollection.find(query) 59 | const result = await cursor.toArray(); 60 | res.send(result); 61 | }) 62 | // Get Function 63 | app.get('/countries', async (req, res) => { 64 | const cursor = countriesCollection.find(); 65 | const result = await cursor.toArray(); 66 | res.send(result); 67 | }) 68 | 69 | app.get('/tourist-spots-country/:country_Name', async(req, res) =>{ 70 | const country_Name = req.params.country_Name; 71 | const query = { country_Name: country_Name}; 72 | const cursor = touristSpotsCollection.find(query); 73 | const result = await cursor.toArray(); 74 | res.send(result); 75 | }) 76 | 77 | app.post('/tourist-spots', async (req, res) => { 78 | const touristInfo = req.body; 79 | const result = await touristSpotsCollection.insertOne(touristInfo); 80 | res.send(result); 81 | }) 82 | 83 | app.patch('/tourist-spots/:id', async (req, res) => { 84 | const id = req.params.id; 85 | const touristInfo = req.body; 86 | const filter = { _id: new ObjectId(id) }; 87 | const updateDoc = { 88 | $set: { 89 | imageURL: touristInfo.imageURL, 90 | tourists_spot_name: touristInfo.tourists_spot_name, 91 | country_Name: touristInfo.country_Name, 92 | location: touristInfo.location, 93 | description: touristInfo.description, 94 | average_cost: touristInfo.average_cost, 95 | seasonality: touristInfo.seasonality, 96 | travel_time: touristInfo.travel_time, 97 | totalVisitorsPerYear: touristInfo.totalVisitorsPerYear, 98 | } 99 | } 100 | const result = await touristSpotsCollection.updateOne(filter, updateDoc); 101 | res.send(result); 102 | }) 103 | 104 | app.delete('/tourist-spots/:id', async (req, res) => { 105 | const id = req.params.id; 106 | const query = { _id: new ObjectId(id) }; 107 | const result = await touristSpotsCollection.deleteOne(query); 108 | res.send(result); 109 | }) 110 | 111 | // Send a ping to confirm a successful connection 112 | await client.db("admin").command({ ping: 1 }); 113 | console.log("Pinged your deployment. You successfully connected to MongoDB!"); 114 | } finally { 115 | // Ensures that the client will close when you finish/error 116 | // await client.close(); 117 | } 118 | } 119 | run().catch(console.dir); 120 | 121 | 122 | app.get('/', (req, res) => { 123 | res.send('Your Server is Running'); 124 | }) 125 | 126 | app.listen(port, () => { 127 | console.log(`Server is running on: http://localhost:${port}/`); 128 | }) --------------------------------------------------------------------------------