-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.17 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
const express = require('express');
const Redis = require('redis');
const redisClient = Redis.createClient();
(async () => {
redisClient.on('error', (err) => console.log('Redis Client Error', err));
await redisClient.connect();
})();
const axios = require('axios');
const port = 3000;
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.get('/photo', async (req, res) => {
redisClient.get('photo', async (err, val) => {
if (err) {
console.error(err);
throw err;
}
if (val) {
res.status(200).send(JSON.parse(val));
} else {
const { photos } = await axios.get('https://jsonplaceholder.typicode.com/photos');
redisClient.set('photo', JSON.stringify(photos));
res.status(200).send(photos);
}
});
});
app.get('/photo/:id', async (req, res) => {
const queryLink = 'https://jsonplaceholder.typicode.com/photos/' + req.params.id;
const { data } = await axios.get(queryLink);
res.json(data);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
});