-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (28 loc) · 1.01 KB
/
index.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
const express = require('express');
const webpush = require('web-push');
const path = require('path');
const app = express();
const port = process.env.PORT || 5001;
const vapidKeys = {
publicKey: 'BPf1QoUlX4Cjqc15wn4j7xUAfKVMZLKqp6cQYRnAwvky2wywBCZx0HAXnnB7pqoMtgkWygPgs5d8h5vpDfRMos8',
privateKey: 'xYuIzOtoYu79CSNrJEjTAedPcoJ0atR4_MPpgZN_cOU'
};
webpush.setVapidDetails(
'mailto:[email protected]',
vapidKeys.publicKey,
vapidKeys.privateKey
);
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public/pwa')));
app.post('/subscribe', (req, res) => {
const subscriptionRequest = req.body;
console.log(subscriptionRequest)
console.log('@body')
webpush.sendNotification(subscriptionRequest, JSON.stringify({ title: "Hello from push notification", description: "This is a test notification" }))
.catch(error => console.error(error));
res.status(200).json({ success: true });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = app;