-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (46 loc) · 1.42 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
import express from 'express';
import morgan from 'morgan';
import helmet from 'helmet';
import dotenv from 'dotenv';
import cors from 'cors';
import logger from 'node-color-log';
import cron from 'cron';
import { getTotalStats, fetchAllStats, getFacebookAccessToken } from './services.js';
dotenv.config();
const { CronJob } = cron;
const job = new CronJob('*/10 * * * *', async () => {
logger.info('Fetching all stats');
await fetchAllStats();
});
job.start();
const app = express();
app.use(cors());
app.use(morgan('combined'));
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ['*'],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'", 'http://unpkg.com/vue@next'],
styleSrc: ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com/css', 'https://use.fontawesome.com/releases/v5.12.0/css/all.css'],
objectSrc: ["'none'"],
upgradeInsecureRequests: [],
},
},
}));
app.use(express.json());
app.use(express.static('./public'));
app.get('/stats', async (req, res) => {
const stats = await getTotalStats();
res.status(200).jsonp(stats);
});
app.get('/fb_token', async (req, res) => {
try {
await getFacebookAccessToken(req.query.token);
await fetchAllStats();
res.status(200).end();
} catch (error) {
res.status(400).send(error.message);
}
});
const PORT = process.env.PORT || 3333;
app.listen(PORT, logger.info(`Server is listening on port ${PORT}`));