-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (36 loc) · 1.02 KB
/
app.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
const express = require("express");
const axios = require("axios");
const { createClient } = require("redis");
const app = express();
const PORT = 5555;
const API_URL = "https://jsonplaceholder.typicode.com/todos";
const KEY_NAME = "todo_items";
const EXPIRE_IN_SECONDS = 5;
app.get("/todos", async (req, res) => {
try {
const redisClient = createClient(6379);
await redisClient.connect();
const cached_data = await redisClient.get(KEY_NAME);
if (!cached_data) {
const d = await axios.get(API_URL);
redisClient.setEx(KEY_NAME, EXPIRE_IN_SECONDS, JSON.stringify(d.data));
return res.status(200).send({
error: false,
message: "API response",
data: d.data,
});
} else {
return res.status(200).send({
error: false,
message: "Cached response",
data: JSON.parse(cached_data),
});
}
} catch (error) {
console.log(error);
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
module.exports = app;