-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (81 loc) · 3.26 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express')
const colors = require('colors')
const bodyParser = require('body-parser')
const webPush = require('web-push')
const _ = require('lodash')
const app = express()
// these subscriptions would be set on
// a user in the db - notifications for that user
// would get sent using that subscription
const subscriptions = []
app.use(bodyParser.json())
webPush.setGCMAPIKey('AIzaSyCV18xhRnPeCRrswdiQCsoK10GCXamLR3s')
webPush.setVapidDetails(
'mailto:[email protected]',
'BMKJVS0IzUBTyOUgEDD_AHFseovd7oA4AqyJYfFJTI0d2hBzBsNQY4ptUSG3LLU_PCXXS5lFbUDY0nMGvIDuGrw',
'1cAi0RmoNp_VP5z3452lHBbMP0xPcF8P4shSHieTWgk'
)
function sendNotification(subscription, message) {
const payload = message
webPush.sendNotification(subscription, payload).then(function() {
console.log('[ NOTIFICATION SENT ] :'.bold, `"${payload}" `.yellow.inverse, '|', colors.cyan(subscription.endpoint).underline, '\n')
}).catch(function(error) {
console.log('[ ERROR SENDING NOTIFICATION ] : '.bold, colors.red(error))
subscriptions = _.reject(subscriptions, subscription)
})
}
function isSubscribed(subscription) {
return _.filter(subscriptions, subscription).length
}
app.use(function forceSSL(req, res, next) {
var host = req.get('Host')
var localhost = 'localhost'
if (host.substring(0, localhost.length) !== localhost) {
// https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security
res.header('Strict-Transport-Security', 'max-age=15768000')
// https://github.com/rangle/force-ssl-heroku/blob/master/force-ssl-heroku.js
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect('https://' + host + req.url)
}
}
return next()
})
app.use(function corsify(req, res, next) {
// http://enable-cors.org/server_expressjs.html
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept')
next()
})
app.use(function setServiceWorkerHeader(req, res, next) {
// https://github.com/mozilla/serviceworker-cookbook/issues/201
var file = req.url.split('/').pop()
if (file === 'service-worker.js' || file === 'worker.js') {
res.header('Cache-control', 'public, max-age=0')
}
next()
})
app.post('/register', function(req, res) {
var subscription = req.body.subscription
if (!isSubscribed(subscription)) {
console.log('[ SUBSCRIPTION REGISTERED ] :'.bold, colors.cyan(subscription.endpoint, '\n\n').underline)
subscriptions.push(subscription)
}
res.type('js').send('{"success":true}')
})
app.post('/unregister', function(req, res) {
var subscription = req.body
if (isSubscribed(subscription)) {
console.log('[ SUBSCRIPTION UNREGISTERED ] :'.bold, colors.cyan(subscription.endpoint))
subscriptions = _.reject(subscriptions, subscription)
}
res.type('js').send('{"success":true}')
})
app.get('/send-all', function (req, res) {
const message = req.query.message || 'default message'
res.send(`<h1 style="margin: 30% 0; text-align: center;">SENDING ALL SUBSCRIBERS A MESSAGE: ${ message }</h1>`)
subscriptions.forEach(function(subscription){ sendNotification(subscription, message) })
})
app.use(express.static('./'))
app.listen(7001, function () {
console.log('[ SERVER STARTED ] :'.green, 'PORT:7001', '\n')
})