-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
55 lines (42 loc) · 1.62 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
52
53
54
55
require('dotenv').config(); // load the environment variables
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const appRoot = require('app-root-path');
const path = require('path');
global.appRoot = appRoot;
if( process.env.DB_USER == null
|| process.env.DB_PASSWORD == null
|| process.env.NODE_ENV == "development") {
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true });
} else {
mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@ds145752.mlab.com:45752/savour-dev`, { useNewUrlParser: true });
}
const app = express();
app.use(bodyParser.json());
console.log("port: ", process.env.PORT)
const port = process.env.PORT || 5000;
app.locals.appName = 'Savour';
app.get('/api/hello', (req, res) => {
res.send({ express: 'Hello From Savour Backend' });
});
// FORMAT OF TOKEN
// Authorization: Bearer <access_token>
// set up the set the routes defined in /server/routers to be endpoint
require('./server/routes')(app);
// production
if (process.env.NODE_ENV === 'production') {
console.log("running in production");
console.log("__dirname", __dirname);
// 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'));
});
}
// catch other routes and send 404
app.get("*", (req, res) => {
res.status(404).send("Route does not exist");
})
app.listen(port, () => console.log(`Listening on port ${port}`));