-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (44 loc) · 1.59 KB
/
app.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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
require('dotenv/config');
const PORT = 3001;
const app = express();
const authRouter = require('./routes/auth');
const invitationRouter = require('./routes/invitation');
const tradesRouter = require('./routes/trades');
const rateLimit = require('express-rate-limit');
const sslRedirect = (environments = ['production'], status = 302) => {
const currentEnv = process.env.NODE_ENV;
const isCurrentEnv = environments.includes(currentEnv);
return (req, res, next) => {
if (isCurrentEnv) {
req.headers['x-forwarded-proto'] !== 'https'
? res.redirect(status, 'https://' + req.hostname + req.originalUrl)
: next();
} else next();
};
};
const invitationLimiter = rateLimit({
windowMs: parseInt(process.env.INVITATION_RATE_LIMITER_WINDOW) * 1000,
max: parseInt(process.env.INVITATION_RATE_LIMITER_RATES),
message: 'Too many invitations from this IP, please try again after an hour',
});
// Register middleware
app.use(cors());
app.use(express.json());
app.use('/api/auth', authRouter);
app.use('/api/invitation', invitationLimiter, invitationRouter);
app.use('/api/trades', tradesRouter);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res, next) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// Connect To Database
mongoose.connect(process.env.DB_URI, { useNewUrlParser: true }, () => {
console.log('mongoDB Connected');
});
app.listen(PORT);