-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (53 loc) · 1.43 KB
/
index.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
72
73
74
let express = require('express');
let cors = require('cors')
let app = express()
let bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())
const store = {
}
let subscribers = {};
function onSubscribe(req, res) {
res.setHeader('Content-Type', 'text/plain;charset=utf-8');
res.setHeader("Cache-Control", "no-cache, must-revalidate");
const {id, initial} = req.query;
if(initial && Object.keys(initial).length) return res.json(store);
subscribers[id] = res;
console.log('## subscribing to id', id)
console.log('--- subscribers:', Object.keys(subscribers))
console.log(req.query)
req.on('close', function() {
console.log('### closing request for id', id)
delete subscribers[id];
});
}
function publish() {
for (let id in subscribers) {
let res = subscribers[id];
res.json(store);
}
subscribers = {};
}
app.get('/subscribe', (req, res) => {
onSubscribe(req, res);
})
app.get('/test', (req, res) => {
res.json({
success: true
})
})
app.post('/publish', (req, res) => {
// accept POST
req.setEncoding('utf8');
const { key, content } = req.body;
store[key] = content
publish();
res.end("ok");
return;
})
const port = process.env.PORT || 8080
app.listen(port, function () {
console.log('web server listening on port', port)
})