-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
64 lines (53 loc) · 1.64 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
const express = require("express");
const axios = require("axios");
const redis = require("redis");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
// setup redis client
const client = redis.createClient({
port: process.env.REDIS_PORT,
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
});
// redis store configs
const usersRedisKey = "store:users"; // cahce key for users
const dataExpireTime = 3600; // 1 hour cache expire time
// main endpoint
app.get("/", (req, res) =>
res.send("Welcome to Node.js + redis boilerplate API.")
);
// users endpoint with caching
app.get("/users", (req, res) => {
// try to fetch the result from redis
return client.get(usersRedisKey, (err, users) => {
if (users) {
return res.json({ source: "cache", data: JSON.parse(users) });
// if cache not available call API
} else {
// get data from remote API
axios
.get("https://jsonplaceholder.typicode.com/users")
.then((res) => res.data)
.then((users) => {
// save the API response in redis store
client.setex(usersRedisKey, 3600, JSON.stringify(users));
// send JSON response to client
return res.json({ source: "api", data: users });
})
.catch((error) => {
// send error to the client
return res.json(error.toString());
});
}
});
});
// user details endpoint
app.get("/", (req, res) =>
res.send("Welcome to Node.js + Redis boilerplate API.")
);
// start express server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log("Server listening on port:", PORT);
});