-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (54 loc) · 1.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const cors = require('cors');
//const morganBody = require('morgan-body');
const mongoose = require('mongoose');
const app = express();
const dev = app.get('env') !== 'production';
const sslRedirect = require('heroku-ssl-redirect');
const PORT = process.env.PORT || 3001;
let whitelist = ['https://whencanmeet.herokuapp.com', 'https://whencanmeet.benedictpak.com'];
// SSL only enabled for production by default
app.use(sslRedirect());
if (dev) {
whitelist.push('http://localhost:3000');
app.use(morgan('dev'));
//morganBody(app);
mongoose.connect('mongodb://127.0.0.1:27017/whenmeet', { useNewUrlParser: true });
} else {
app.disable('x-powered-by');
app.use(morgan('common'));
app.use(express.static(path.resolve(__dirname, 'client/build')));
mongoose.connect(process.env.MONGOLAB_URI, { useNewUrlParser: true, dbName: 'whenmeet' });
}
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB connection established successfully');
});
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.enable('trust proxy');
app.use(cors(corsOptions));
app.use(express.json());
// Simple middleware for IP logging
app.use(function(req, res, next) {
req.clientIP = (req.headers['x-forwarded-for'] || req.connection.remoteAddress || '').split(',')[0].trim();
next();
});
app.use('/api', require('./routes'));
// For any other requests
app.get('*', (req, res) =>{
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
module.exports = app;