-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (38 loc) · 1.22 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
//Dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require ('body-parser');
const dotenv = require('dotenv').config({path: './server/.env'});
const mongoose = require('mongoose');
//Connect to DB
const DB_URI_MAP = {
"DEVELOPMENT" : process.env.MLAB_DEV_DB_URI,
"PRODUCTION" : process.env.MLAB_PROD_DB_URI,
}
mongoose.connect(DB_URI_MAP[process.env.ENVIRONMENT] || process.env.MLAB_DEV_DB_URI, {
useMongoClient: true
});
const db = mongoose.connection;
mongoose.Promise = global.Promise;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
//Routers
const api = require('./server/routes/api');
const index = require('./server/routes/index');
const app = express();
//Parsers for POST
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
//Point to static
app.use(express.static(path.join(__dirname, 'build')));
//API route
app.use('/api', api);
//All other routes go to index
app.use('*', index);
//set port and initialize
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => {
console.log(`app running on localhost:${port}`);
});