-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
28 lines (22 loc) · 804 Bytes
/
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
const express = require('express')
const redis = require('redis')
const util = require('util')
const { Pool } = require('pg')
const pgClient = new Pool()
// create express application instance
const app = express()
const redisClient = redis.createClient({host: 'redis'})
const get = util.promisify(redisClient.get).bind(redisClient);
const set = util.promisify(redisClient.set).bind(redisClient);
app.get('/', async (req, res) => {
const redisData = await get('latestposts')
if (redisData) {
return res.json({source: 'redis', data: JSON.parse(redisData)})
}
const data = await pgClient.query('select * from posts')
await set('latestposts', JSON.stringify(data.rows), 'EX', 10)
res.json({source: 'pg', data: data.rows});
});
app.listen(3000, () => {
console.log('listening');
});