-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (47 loc) · 1.41 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const history = require('connect-history-api-fallback');
const path = require('path');
const cors = require('cors');
const app = express();
function log(...args) {
if (process.env.DEV)
console.log(...args);
}
/**
* Initialize the app
*/
app.use(cors({ credentials: true, origin: true }));
const historyMiddleware = history();
// Do redirect /api calls to front-end
app.use((req, res, next) => {
if (req.path.startsWith('/api'))
next();
else
historyMiddleware(req, res, next);
});
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/api', require('./api/app'));
app.use(express.static(path.join(__dirname, 'front', 'dist')));
app.use((req, res, next) => {
res.status(404).end('Not found');
});
function startApp() {
app.listen(process.env.PORT || 3000, function () {
log(`Server running on port ::${this.address().port}`);
});
}
const mongoOpts = {
useNewUrlParser: true,
useUnifiedTopology: true,
reconnectInterval: 30000, // Try to reconnect after 30s
reconnectTries: 600, // Just be a little more persistent (try for 5 hours)
};
/**
* Start the app after mongoose connected
*/
mongoose.connect(process.env.DB_URL, mongoOpts)
.then(() => startApp())
.catch(err => log(err));