-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver2.js
36 lines (28 loc) · 1.1 KB
/
server2.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const app = express();
const port = process.env.PORT || 5000;
// use body parser to get data from POST requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Use API routes from the api folder
const apis = require("./api");
app.use("/api", apis);
// If in production, then use static frontend build files.
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, '../client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, '../client/build', 'index.html'));
});
}
// Connect to Mongo
// mongoose.connect(process.env.DB, {
// useNewUrlParser: true,
// useUnifiedTopology: true
// }).then(() => console.log('MongoDB Connected...')).catch(err => console.log(err));
app.listen(port, () => console.log(`Listening on port ${port}`));