-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (35 loc) · 1.18 KB
/
Copy pathserver.js
File metadata and controls
46 lines (35 loc) · 1.18 KB
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
const express = require('express')
const cors = require('cors')
const { default: axios } = require('axios')
const Cache = require('./cache')
// initializing instances
const app = express()
const cache = new Cache()
// middlewares
app.use(express.json())
app.use(cors())
// route handlers
app.post('/api/v1/is-fake', async (req,res) => {
// fetch user's data from cache
const userCache = await cache.get(req.body.username)
let isFake = null;
if(userCache !== null){ // cache hit
isFake = userCache
}else if(req.body.user !== null && req.body.user !== undefined ){ // cache miss
// fetch the result from the sagemaker
const response = await axios.post('https://wl1nlfxw9f.execute-api.ap-south-1.amazonaws.com/prod', {
input: Object.values(req.body.user)
})
isFake = response.data
// set the result to the cache
cache.set(req.body.username, response.data)
}
res.send(isFake)
})
// listening
app.listen(3000, async () => {
console.log("Listening to port 3000");
// connecting to the redis server
await cache.connect()
console.log('Connection with the redis server established')
})