-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (60 loc) · 2.45 KB
/
app.js
File metadata and controls
79 lines (60 loc) · 2.45 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
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
79
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const cron = require('node-cron');
const webpush = require('web-push');
const authRoutes = require('./routes/auth');
const siteRoutes = require('./routes/site');
const subscriptionRoutes = require('./routes/subscription');
const notificationRoutes = require('./routes/notification');
const appRoutes = require('./routes/app');
const HttpError = require('./utils/http-error');
const { errorHandler } = require('./middlewares/errorHandler');
const Authorization = require('./middlewares/authorize');
const { getRouteList } = require('./utils/utils');
const Notification = require('./utils/sendNotifications');
require('dotenv').config();
const PORT = process.env.PORT || 5000;
const app = express();
// json parser
app.use(bodyParser.json());
// Allow CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, OPTIONS');
next();
});
// authorization check middleware
app.use(Authorization.checkPermission);
// Routes
app.get('/', (req, res) => {
res.send('Requisting from ' + req.ip + ' ip address, go to /api/${action} to see the available actions');
});
app.use('/api/auth', authRoutes);
app.use('/api/subscription', subscriptionRoutes);
// app.use('/api/notification', notificationRoutes);
app.use('/api/app', appRoutes);
app.use('/api', siteRoutes);
// save route list to json file
fs.writeFile('./utils/routeList.json', JSON.stringify(getRouteList(app)), (err) => {
if (err) {
return console.log(err);
}
});
// if route not found
app.use((req, res, next) => {
throw new HttpError('Could not find this route', 404);
});
// this error middleware needs to be at the bottom of all routes
app.use(errorHandler);
// start the express server
app.listen(PORT, () => console.log(`Server started on port http://localhost:${PORT}`));
const publicVapidKey = 'BJthRQ5myDgc7OSXzPCMftGw-n16F7zQBEN7EUD6XxcfTTvrLGWSIG7y_JxiWtVlCFua0S8MTB5rPziBqNx1qIo';
const privateVapidKey = '3KzvKasA2SoCxsp0iIG_o9B0Ozvl1XDwI63JRKNIWBM';
webpush.setVapidDetails('mailto:test@test.com', publicVapidKey, privateVapidKey);
const cronRunner = async () => {
console.log('running cron job at ' + new Date());
Notification.send();
};
cron.schedule('* * * * *', cronRunner);