-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
107 lines (96 loc) · 2.86 KB
/
server.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const express = require('express')
const {createProxyMiddleware} = require('http-proxy-middleware')
//require newRelic -- decorates req / response object
const app = express()
app.use(express.json())
app.use(express.urlencoded({extended: true}))
// app.use('/ratings', createProxyMiddleware({target: 'http://localhost:5000'}))
// const redis = require('redis')
// const redisPort = 6379
// const client = redis.createClient({
// host: '54.147.168.66',
// port: redisPort,
// legacyMode: true
// })
// client.connect()
// client.on('error', (err) => {
// console.log(err)
// })
const model = require('./index.js')
const port = 5000
const {getReviews, getCharacteristicReviews, postReview, updateHelpfulness, updateReported} = require('./dbMethods')
app.get('/loaderio-d31975cfff2e598ccb1a6a1760dc0426.txt', (req, res) => {
res.send('loaderio-d31975cfff2e598ccb1a6a1760dc0426')
})
app.get('/ratings/reviews/:productId', (req, res) => {
const {productId} = req.params
// client.get(productId, async (err, reviews) => {
// if (err) throw err;
// if (reviews) {
// res.status(200).send({
// reviews: JSON.parse(reviews),
// message: "data retrieved from cache"
// })
// } else {
getReviews(req.params.productId)
.then((response) => {
// client.setex(productId, 600, JSON.stringify(response));
// console.log('ratings response in server', response)
res.send(response).status(200)
})
.catch(err => {
console.log('err in app.get /ratings', err.message)
res.sendStatus(500)
})
})
// }
// })
app.get('/ratings/characteristics/:productId', (req, res) => {
getCharacteristicReviews(req.params.productId)
.then((response) => {
// console.log('char response in server', response)
res.send(response).status(200)
})
.catch(err => {
console.log('err in app.get characteristics', err)
res.sendStatus(500)
})
});
app.post('/ratings/reviews', (req, res) => {
console.log('req.body in server', req.body)
postReview(req.body)
// .then(console.log('post resolved'))
.then(() => {
res.sendStatus(200)
})
.catch(err => {
console.log('err in app.get characteristics', err)
res.sendStatus(500)
})
})
app.put('/ratings/helpful/:reviewId', (req, res) => {
console.log('req.params', req.params)
updateHelpfulness(req.params.reviewId)
.then(() => {
res.sendStatus(200)
})
.catch(err => {
console.log('err in app.get characteristics', err)
res.sendStatus(500)
})
})
app.put('/ratings/report/:reviewId', (req, res) => {
console.log('req.params', req.params)
updateReported(req.params.reviewId)
.then(() => {
res.sendStatus(200)
})
.catch(err => {
console.log('err in app.get characteristics', err)
res.sendStatus(500)
})
})
app.listen(port, () => {
console.log(`listening on localhost:${port}`)
})
module.exports = app;