-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (69 loc) · 2.44 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
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
// B"H
const http = require('http');
const https = require('https');
const url = require('url');
const webPush = require("web-push");
const { URLSearchParams } = require('url');
var vapidKeys = {
"publicKey": "BMFDU3eBLj5pJKRz9lTkadYlfURJRHs0lEe8QB1aMY8yyoS5VhpB9w76b71hrykAxDwOZEFPMj5zglw6HB9uYDI",
"privateKey": process.env.vapidPrivate
}
var VAPID_PUBLIC_KEY = vapidKeys.publicKey;
var VAPID_PRIVATE_KEY = vapidKeys.privateKey;
// Configure VAPID details for web-push
webPush.setVapidDetails(
"https://awtsmoos.com/",
VAPID_PUBLIC_KEY,
VAPID_PRIVATE_KEY
);
const server = http.createServer((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
if (req.method === "OPTIONS") {
// Handle preflight requests
res.writeHead(200);
res.end();
return;
}
if (req.method === "GET") {
if(req.url === "/vapidPublicKey") {
// Return the VAPID public key
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(VAPID_PUBLIC_KEY);
return;
}
} else if (req.method === "POST" && req.url === "/sendNotification") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
try {
const { subscription, payload, ttl, delay } = JSON.parse(body);
const options = { TTL: ttl };
try {
await webPush
.sendNotification(subscription, payload, options)
res.writeHead(201);
res.end("Notification sent successfully.");
return;
} catch((error) => {
console.error("Notification error:", error);
res.writeHead(500);
res.end("Failed to send notification.");
return;
}
} catch (error) {
console.error("Invalid request body:", error);
res.writeHead(400);
res.end("Invalid request body");
return
}
});
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Proxy server running at http://localhost:${PORT}/`);
});