-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (29 loc) · 1.06 KB
/
index.js
File metadata and controls
40 lines (29 loc) · 1.06 KB
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
require('dotenv').config({ path: '.env' });
const express = require('express');
const webPush = require('web-push');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const publicVapidKey = process.env.PUBLIC_VAPID_KEY;
const privateVapidKey = process.env.PRIVATE_VAPID_KEY;
webPush.setVapidDetails('mailto:test@example.com', publicVapidKey, privateVapidKey);
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
const users = new Map();
app.post('/subscribe', (req, res) => {
users.set(req.body.keys.auth, req.body);
res.status(201).json({});
});
app.get('/test', (req, res) => {
const payload = 'Push notifications with Service Workers'
users.forEach(user =>
setTimeout(() => {
webPush.sendNotification(user, payload)
.catch(error => console.error(error))
}, Math.floor(Math.random() * Math.floor(5000)))
);
res.status(200).json(Array.from(users));
});
app.listen(process.env.PORT, function () {
console.log(`Listening on port ${process.env.PORT}`);
});