forked from cmda-minor-web/progressive-web-apps-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (39 loc) · 1.23 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
require("dotenv").config()
const
express = require('express'),
mongoose = require('mongoose'),
db = mongoose.connection,
Subscription = require('./src/models/subscription'),
app = express(),
webPush = require('web-push'),
compression = require('compression'),
cookieParser = require('cookie-parser'),
router = require('./src/router'),
checkNotification = require('./src/utils/checkNotification'),
port = 3000
mongoose.connect(process.env.DB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true
})
db.once('open', _ => {
console.log('Connected to MongoDB!')
})
app
.set('view engine', 'ejs')
.set('views', __dirname + '/src/views/')
.use(express.json())
.use(express.urlencoded({ extended: true }))
.use(cookieParser())
.use(compression())
.use(express.static(__dirname + '/src/static'))
.listen(process.env.PORT || port)
webPush.setVapidDetails('mailto:replacethislater@test.com', process.env.PUBLIC_VAPID, process.env.PRIVATE_VAPID)
setInterval(async _ => {
const subscriptions = await Subscription.find({})
subscriptions.forEach(async subscription => {
if (await checkNotification(subscription)) {
Subscription.find({ _id: subscription.id }).remove().exec()
}
})
}, 60000)
app.use(router)