-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
44 lines (38 loc) · 1.18 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const http = require('http')
const timers = require('timers')
const fluffaction = require('./fluffaction')
const notification = require('./notification')
const PORT = 3872
module.exports = () => {
// Heartbeat every 30 seconds
timers.setInterval(() => {
notification({ type: 'tick' })
}, 30*1000)
http.createServer((req, res) => {
let fragments = req.url.substring(1).split('/')
let query = fragments.splice(0, 2)
query.push(fragments.join('/'))
if (query[0] === 'notification') {
res.writeHead(200, {'Content-Type': 'text/plain', 'Access-Control-Allow-Origin' : '*'})
if (req.method === 'POST') {
let body = ''
req.on('data', function(data) { body += data })
req.on('end', function() {
try {
let json = JSON.parse(body)
notification(json)
res.end("ok")
} catch(e) {
console.log('[Warning] Could not parse notification: ' + e)
res.end('error: ' + e)
}
})
} else {
res.end()
}
} else {
res.writeHead(200, {'Content-Type': 'text/plain', 'Access-Control-Allow-Origin' : '*'})
res.end()
}
}).listen(PORT)
}