-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (58 loc) · 1.72 KB
/
server.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
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
/* eslint-disable import/extensions */
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const cron = require('node-cron');
const connectDB = require('./config/db');
const { handleError } = require('./services/handleError');
const cronJob = require('./api/utils/cronJob');
const User = require('./api/models/User');
const webPush = require('./config/webPush');
const app = express();
app.use(
cors({
origin: '*',
})
);
app.use(express.json({ extended: false }));
app.use(
morgan('tiny', {
skip: req => {
return req.path === '/' && req.method === 'GET';
},
})
);
connectDB();
webPush();
const PORT = process.env.PORT || 4000;
app.get('/', (req, res) => res.send(`Server Up and running v${process.env.npm_package_version}`));
app.use('/api', require('./api/index.js'));
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
handleError(err, req, res);
});
// cron.schedule('*/15 * * * *', () => {
// fetch('https://immense-hamlet-02246.herokuapp.com')
// .then(res => console.log(`successful job: ${res.ok}`))
// .catch(err => console.log(err));
// });
cron.schedule('5 0 * * *', async () => {
const users = await User.find({});
const allPromises = users.map(agent => {
return cronJob({
name: agent.name,
email:
process.env.NODE_ENV === 'production'
? agent.email
: '[email protected]',
agentId: agent.userId,
});
});
await Promise.all(allPromises);
});
app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log('Server(v1.0) is running on ', PORT);
});