-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (48 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express')
const { MongoClient } = require('mongodb');
const ObjectId = require('mongodb').ObjectId;
require('dotenv').config();
const app = express()
const cors = require('cors');
const port = process.env.PORT || 5000;
// MiddleWare
app.use(cors());
app.use(express.json())
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.fq8sq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function run() {
try {
await client.connect();
const database = client.db("booking");
const booking = database.collection("booking");
// post api
app.post('/booking', async (req, res) => {
const service = req.body;
const result = await booking.insertOne(service);
console.log(result)
res.json(result)
})
// get api post
app.get('/booking', async (req, res) => {
const cursor = booking.find({});
const tourService = await cursor.toArray();
res.send(tourService)
})
// single post api
app.get('booking/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) }
const tourService = await booking.findOne(query)
res.json(tourService)
})
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.get('/', (req, res) => {
res.send('Hello i am ready')
})
app.listen(port, () => {
console.log('hello booking website ? ')
})