-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
59 lines (45 loc) · 1.53 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
51
52
53
54
55
56
57
58
59
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var globalNotifications = [];
var notificationsPerOrganization = [];
function getNotifsForOrganization(organizationId) {
var notifs = notificationsPerOrganization[organizationId];
if (notifs === undefined) {
notifs = new Array();
}
return notifs;
};
app.get('/', function(req, res) {
res.send('notifwebhook is running.');
});
app.get('/notifications', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(globalNotifications));
});
app.get('/notifications/:organizationid', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(getNotifsForOrganization(req.params.organizationid)));
});
app.post('/notifications', function(req, res) {
console.log("Received a notification.");
if (req.body.organizationId !== undefined) {
var notifs = getNotifsForOrganization(req.body.organizationId);
notifs.push(req.body);
notificationsPerOrganization[req.body.organizationId] = notifs;
} else {
globalNotifications.push(req.body);
}
res.sendStatus(200);
});
app.delete('/notifications', function(req, res) {
console.log("Clearing notifications.");
globalNotifications = [];
notificationsPerOrganization = [];
res.sendStatus(200);
});
app.listen(port, function() {
console.log('Server listening.')
});