-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
83 lines (74 loc) · 1.89 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
// require('newrelic');
const express = require('express');
const bodyParser = require('body-parser');
const helpers = require('./database/psql.js');
const port = 3002;
const cors = require('cors');
const axios = require('axios');
const redis = require("redis");
const redisPort = 6379
const client = redis.createClient(redisPort);
const app = express();
client.on("error", (err) => {
console.log(err);
});
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
app.get('/products/:shoeId/summary', (req, res) => {
const id = req.params.shoeId;
try {
client.get(id, async (err, jobs) => {
if (err) throw err;
if (jobs) {
res.status(200).send(JSON.parse(jobs));
} else {
await helpers.getShoeData(id, (err, doc) => {
if (err) {
res.sendStatus(404);
}
client.setex(id, 600, JSON.stringify(doc.rows[0]));
res.status(200).send(doc.rows[0]);
});
}
})
} catch (err) {
res.status(404).send()
}
// helpers.getShoeData(id, (err, doc) => {
// if (err) {
// res.sendStatus(404);
// }
// res.status(200).send(doc.rows[0]);
// });
});
app.post('/products', (req, res) => {
console.log(req.body);
helpers.createShoeData( req.body, (err, data) => {
if (err) {
res.send(err).status(406);
} else {
res.send('Success').status(200);
}
})
})
app.put('/products', (req, res) => {
helpers.updateShoeData(req.query.id, req.body, (err, shoe) => {
if (err) {
res.send(err).status(406);
} else {
res.send(shoe).status(200);
}
})
})
app.delete('/products', (req, res) => {
helpers.deleteShoeData(req.query.id, (err, shoe) => {
if (err) {
res.send(err).status(406);
} else {
res.send(shoe).status(200);
}
})
})
module.exports = app;