-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (64 loc) · 1.81 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
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
const express = require('express')
const app = express()
const redis = require('redis')
const bodyParser = require('body-parser')
const cors = require('cors')
const axios = require('axios')
const client = redis.createClient('redis://localhost:6379')
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
// both functions get the original number from the user input, and the res object
// from express, to make a redirect to another URL as params
const getResultFromCache = (number, res) => {
client.get(number, (error, result) => {
res.redirect('/done?result=' + result + '&from=cache')
})
}
const getResultFromAPI = (number, res) => {
axios
.post('http://localhost:3000/', {
number: number
})
.then(response => {
let result = response.data.result
// when receiving the result from API, original number from the user input and the result will be stored in the CACHE
client.set(number, result)
// the cache entry will be deleted after 60 sec, automatically
client.expire(number, 60)
res.redirect('/done?result=' + result + '&from=API')
})
.catch(error => {
console.log(error)
})
}
app.post('/', (req, res) => {
let number = req.body.number
// if the original number and the result are already stored, result will be true
client.exists(number, (error, result) => {
if (result) {
getResultFromCache(number, res)
// else we will make a request to the API
} else {
getResultFromAPI(number, res)
}
})
})
app.get('/done', (req, res) => {
res.send(`
<html>
<head>
</head>
<body>
The Result is: ${req.query.result}
<br/>
So the original value is ${req.query.result / 2}
<br/>
And comes from: ${req.query.from}
</body>
</html>
`)
})
app.listen(8080)