-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (63 loc) · 2.24 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
const express = require("express");
const app = express();
require('dotenv').config();
const cors = require('cors');
const ObjectId = require("mongodb").ObjectId;
const { MongoClient } = require('mongodb');
app.use(cors());
app.use(express.json());
// tour-users
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.yhxur.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
const port = process.env.PORT || 8000;
app.get('/', (req, res) => {
res.send('Running Tour server')
});
async function run() {
try {
await client.connect();
const database = client.db("tour-web");
const servicesCollection = database.collection("services");
const addOrdersCollection = database.collection("orders");
// get api
app.get('/services', async (req, res) => {
const cursor = servicesCollection.find({});
const services = await cursor.toArray();
res.send(services);
});
//post service
app.post('/services', async (req, res) => {
const service = req.body;
console.log('post api', service);
const result = await servicesCollection.insertOne(service);
console.log(result);
res.json(result)
});
//for orders
app.get('/orders', async (req, res) => {
const cursor = addOrdersCollection.find({});
const orders = await cursor.toArray();
console.log('orders')
res.json(orders);
});
//post orders
app.post('/orders', async (req, res) => {
const orders = req.body;
const result = await addOrdersCollection.insertOne(orders);
console.log(result);
res.json(result)
});
app.delete('/services/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await servicesCollection.deleteOne(query);
res.json(result);
})
} finally {
// await client.close();
}
}
run().catch(console.dir);
app.listen(port, () => {
console.log('Running ', port);
})