-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (33 loc) · 1.2 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
// CMS Server
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const cors = require('cors');
const passport = require('passport');
const apiRoutes = require('./routes');
const { SpecialCard } = require('./models');
const MONGO_URI = process.env.BOG_DB_URI || '';
const PORT = process.env.NODE_ENV === "production" ? 80 : 6660;
const app = express();
dotenv.load();
mongoose.connect(MONGO_URI, { useNewUrlParser: true })
.then(onDBConnect)
.catch(err => console.error(err));
// middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.resolve(__dirname, 'build')));
app.use(passport.initialize());
require('./config/passport')(passport);
app.use('/api', apiRoutes);
// serve index.html and let front end router handle everything...
app.get('*', (req, res, next) => {
if (req.url.includes('/api/')) next(); // ...except /api
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
app.listen(PORT, () => console.log('server up and listening on port', PORT));
function onDBConnect() {
console.log('successfully connected to MongoDB instance');
}