-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (57 loc) · 1.96 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
68
69
70
71
72
73
74
75
76
const express = require('express')
const app = express()
const port = process.env.PORT || 5000
const cors = require('cors')
const bodyParser = require('body-parser')
require('dotenv').config()
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.use(cors())
app.use(express.json())
const MongoClient = require('mongodb').MongoClient;
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ps1cs.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const serviceCollection = client.db("cleanAndShine").collection("services");
const testimonialCollection = client.db("cleanAndShine").collection("reviews");
console.log('connected', err)
// add services server code end here
app.post('/addService', (req, res) => {
const newService = req.body;
console.log('adding new service', newService)
serviceCollection.insertOne(newService)
.then(result => {
console.log(result.insertedCount)
res.send(insertedCount > 0)
})
})
app.get('/service', (req, res) => {
serviceCollection.find()
.toArray((err, items) => {
res.send(items)
// console.log('from database items', items)
})
})
// add services server code end here
// add review server code end here
app.post('/addTestimonial', (req, res) => {
const newTestimonial = req.body;
console.log('adding new review', newTestimonial)
testimonialCollection.insertOne(newTestimonial)
.then(result => {
console.log(result.insertedCount)
res.send(insertedCount > 0)
})
})
app.get('/testimonial', (req, res) => {
testimonialCollection.find()
.toArray((err, review) => {
res.send(review)
console.log('from database items', review)
})
})
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})