-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (33 loc) · 935 Bytes
/
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
const express = require("express");
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
const cors = require("cors");
const webpush = require("web-push");
const app = express();
dotenv.config();
app.use(cors());
app.use(bodyParser.json());
webpush.setVapidDetails(
process.env.WEB_PUSH_CONTACT,
process.env.PUBLIC_VAPID_KEY,
process.env.PRIVATE_VAPID_KEY
);
app.get("/", (req, res) => {
res.send("Hello world!");
});
app.post("/notifications/subscribe", (req, res) => {
const subscription = req.body;
console.log(subscription);
const payload = JSON.stringify({
title: "Hello!",
body: "It works.",
});
webpush
.sendNotification(subscription, payload)
.then((result) => console.log(result))
.catch((e) => console.log(e.stack));
res.status(200).json({ success: true });
});
app.listen(9000, () =>
console.log("The server has been started on the port 9000")
);