-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
50 lines (40 loc) · 1.11 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
const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors')
const app = express();
app.use(cors())
app.use(logger('dev'));
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
let clients = []
app.get('/', (req, res) => {
res.json(req.body)
});
app.get('/events', (req, res) => {
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
};
res.writeHead(200, headers);
res.write('data: {}\n\n')
const clientId = Date.now();
clients.push({
id: clientId,
response: res
})
req.on('close', () => {
clients = clients.filter(client => client.id !== clientId);
});
})
app.post('/post', (req, res) => {
res.json(req.body)
const data = JSON.parse(req.body?.["m2m:sgn"]?.["m2m:nev"]?.["m2m:rep"]?.["m2m:cin"]?.["con"] || '{}')
const output = `data: ${JSON.stringify(data)}\n\n`
return clients.forEach(client => {
client.response.write(output)
})
})
module.exports = app;