-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
50 lines (38 loc) · 1.29 KB
/
main.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
require('dotenv').config()
const express = require("express")
const pg = require("pg")
const { createClient } = require("redis")
const responseTime = require("response-time")
const { promisify } = require("util")
const connection = new pg.Pool({
connectionString: process.env.DB_URL || process.env.LOCAL_DB_URL,
ssl: process.env.DB_URL ? true : false
})
const client = createClient({
host: 'red-clh3lu58td7s73bm67ag',
port: 6379,
});
client.on("connect", () => console.log(`connected ${client.connected}`));
client.on("error", (err) => console.log(err));
const GET_ASYNC = promisify(client.get).bind(client)
const SET_ASYNC = promisify(client.set).bind(client)
const PORT = process.env.PORT || 3000
const app = express()
app.use(responseTime())
app.get('/', async (req, res) => {
try {
const reply = await GET_ASYNC('users')
if (reply) {
console.log('using cash')
res.send(JSON.parse(reply))
return
}
const users = (await connection.query('SELECT * FROM users')).rows
const saveResult = await SET_ASYNC('users', JSON.stringify(users), "EX", 20)
console.log('Set cash')
res.send(users)
} catch (error) {
console.log(error)
}
})
app.listen(PORT, () => console.log('WORK'))