-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
82 lines (74 loc) · 2.87 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
73
74
75
76
77
78
79
80
81
82
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const useragent = require('express-useragent');
const app = express();
const port = process.env.PORT || 3000;
const db = process.env.DB_URI.replace('<PASSWORD>', process.env.DB_PW);
const cron = require('node-cron');
// connect widget db
mongoose
.connect(db)
.then(() => console.log('Connected to MongoDB Database:', mongoose.connection.db.databaseName));
// middleware
process.env.NODE_ENV === 'production'
? app.use(cors({ origin: process.env.FRONTEND_HOSTED }))
: app.use(cors({ origin: process.env.FRONTEND_LOCAL }));
app.use(express.json());
app.use(useragent.express());
// routers & handlers
const userRouter = require('./server/main/routers/userRouter');
const likeRouter = require('./server/main/routers/likeRouter');
const ipHandler = require('./server/widgets/03/ipHandler');
const toDoRouter = require('./server/widgets/04/toDoRouter');
const marketRouter = require('./server/widgets/05/market/marketRouter');
const currencyRouter = require('./server/widgets/05/currency/currencyRouter');
const lotteryRouter = require('./server/widgets/06/lotteryRouter');
const megaMillionRouter = require('./server/widgets/06/megamillions/megaMillionRouter');
const powerballRouter = require('./server/widgets/06/powerball/powerballRouter');
const ghRouter = require('./server/widgets/07/ghRouter');
const bookmarkRouter = require('./server/widgets/08/bookmarkRouter');
app.use('/users/', userRouter);
app.use('/likes/', likeRouter);
app.get('/widget/user-ip-data/', ipHandler.collectUserData, ipHandler.getUserInfo);
app.use('/widget/todos/', toDoRouter);
app.use('/widget/markets/', marketRouter);
app.use('/widget/currencies/', currencyRouter);
app.use('/widget/lottery/', lotteryRouter);
app.use('/widget/lottery/megamillion/', megaMillionRouter);
app.use('/widget/lottery/powerball/', powerballRouter);
app.use('/widget/gh/', ghRouter);
app.use('/widget/bookmark/', bookmarkRouter);
// daily data refresh
const marketData = require('./server/widgets/05/market/marketData');
const currencyData = require('./server/widgets/05/currency/currencyData');
cron.schedule(
'0 11 * * 1-5',
async () => {
await marketData.getFeaturedStocks();
await currencyData.fetchExchangeRate();
},
{ timezone: 'America/New_York' }
);
// monthly data refresh
const lotteryData = require('./server/widgets/06/lotteryData');
cron.schedule(
'0 0 1 * *',
async () => {
await lotteryData.updateLotteryData();
},
{ timezone: 'America/New_York' }
);
// redirect backend host to frontend
app.use((req, res, next) => {
if (req.hostname === 'data.playground.aniqa.dev') {
return res.redirect(process.env.FRONTEND_HOSTED);
}
next();
});
// server
app.listen(port, () => {
console.log(`App running on port: ${port}...`);
console.log(`Environment: ${process.env.NODE_ENV}`);
});