-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
60 lines (49 loc) · 1.46 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
//set up dependencies
const express = require("express");
const redis = require("redis");
const axios = require("axios");
const bodyParser = require("body-parser");
//setup port constants
const port_redis = process.env.PORT || 6379;
const port = process.env.PORT || 5000;
//configure redis client on port 6379
const redis_client = redis.createClient(port_redis);
//configure express server
const app = express();
//Body Parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Middleware Function to Check Cache
checkCache = (req, res, next) => {
const { id } = req.params;
redis_client.get(id, (err, data) => {
if (err) {
console.log(err);
res.status(500).send(err);
}
if (data != null) {
res.send(data);
} else {
next();
}
});
};
// Endpoint: GET /starships/:id
// @desc Return Starships data for particular starship id
app.get("/starships/:id", checkCache, async (req, res) => {
try {
const { id } = req.params;
const starShipInfo = await axios.get(
`https://swapi.co/api/starships/${id}`
);
//get data from response
const starShipInfoData = starShipInfo.data;
//add data to Redis
redis_client.setex(id, 3600, JSON.stringify(starShipInfoData));
return res.json(starShipInfoData);
} catch (error) {
console.log(error);
return res.status(500).json(error);
}
});
app.listen(port, () => console.log(`Server running on Port ${port}`));