-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
38 lines (29 loc) · 913 Bytes
/
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
// Importing Modules
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
// importing files
const routes = require('./routes');
// Define Global Variables
const app = express();
const log = console.log;
const PORT = process.env.PORT || 8080; // Step 1
// Step 2
mongoose.connect( process.env.MONGODB_URI || 'mongodb://localhost/my_database', {
useNewUrlParser: true
});
// Configuration
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', routes);
// Step 3
if (process.env.NODE_ENV === 'production') {
app.use(express.static( 'client/build' ));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html')); // relative path
});
}
app.listen(PORT, () => {
log(`Server is starting at PORT: ${PORT}`);
});