-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
52 lines (44 loc) · 1.35 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 mongoose = require('mongoose');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const dotenv = require('dotenv').config();
mongoose.Promise = global.Promise;
// fix eslint no console
const logger = console;
const corsOptions = {
'origin': true,
'methods': 'GET, HEAD, PUT, PATCH, POST, DELETE',
'preflightContinue': true,
'optionsSuccessStatus': 204,
'credentials': true // enable set cookie
};
const app = express();
// static server
app.use(express.static(path.join(__dirname, 'client/build')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors(corsOptions));
mongoose.Promise = global.Promise;
const connect = mongoose.connect(
process.env.MONGODD_URI,
{ useMongoClient: true }
);
connect.then(() => {
const { routes } = require('./api/routes/routes');
routes(app);
app.get('*', (req, res) => {
res.redirect('/');
});
/*
catch all for undefined routes, handles client side routing issue on
trying to access pages directly
*/
app.listen(process.env.PORT);
logger.log(`Server listening on port ${process.env.PORT}`);
}, (err) => { // eslint-disable-line
logger.log('\n**********************');
logger.log('ERROR: Failed to connect to MongoDB.');
logger.log('\n**********************');
});