-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
107 lines (94 loc) · 3.29 KB
/
express.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*===================================
||
|| NodeJS Server with Express Framework
||
===================================*/
/*---------------------------
| Environment Vars
---------------------------*/
require('dotenv').config();
/*---------------------------
| Resources
---------------------------*/
const path = require('path');
const express = require('express');
const cookieParser = require('cookie-parser');
const session = require('express-session');
/*---------------------------
| Mongoose
---------------------------*/
const mongoose = require('mongoose');
mongoose.promise = global.Promise;
const mongoConn = process.env.MONGO_DB_CONN;
/* Connecting to Mongo ---------------------------*/
if (mongoConn) {
mongoose
.connect(mongoConn, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false })
.then((res) => {
console.log('Mongo: Connection made.');
})
.catch((err) => {
console.log(`Mongoose Connection Error: ${err}`);
});
} else {
console.log('Missing MONGO_DB_CONN env var for Mongo connection');
}
/*---------------------------
| Express
---------------------------*/
/* Initialize ---------------------------*/
const app = express();
/* JSON support for request response ---------------------------*/
app.use(express.json());
/* Server Session support user access. ---------------------------*/
// initialize cookie-parser to allow us access the cookies stored in the browser.
app.use(cookieParser());
app.use(session({
secret: "Secret Unique Value",
resave: false,
saveUninitialized: true,
}));
/* Serve the static files from the React app ---------------------------*/
app.use(express.static(path.join(__dirname, 'build')));
/*---------------------------
| !IMPORTANT :: Should not be done in Production
| Bypassing CORS so Node Express can be on NODE_PORT 5000 and react can be on 3000
---------------------------*/
if (process.env.NODE_ENV === 'local') {
console.log('Bypassing CORS for Local Development.');
app.use((request, response, next) => {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
}
/*---------------------------
| Route Collections
---------------------------*/
const routes = require('./express-routes/index.js');
app.use('/api/auctions', routes.auctions);
app.use('/api/users', routes.users);
app.use('/api/bidSubmission', routes.bidSubmission);
// Catchall for requests that do not match our routing
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
/*---------------------------
| Port Management
---------------------------*/
const HEROKU_PORT = process.env.PORT;
const EXPRESS_PORT = process.env.EXPRESS_PORT;
const PORT = (EXPRESS_PORT) ? EXPRESS_PORT : HEROKU_PORT;
const FINAL_PORT = (PORT) ? PORT : 5000; // In case none are provided fall back to 5000
/*---------------------------
| Start the Server
---------------------------*/
app.listen(FINAL_PORT, () => {
console.log('Express Server is up and running. Currently listening on port: ' + FINAL_PORT );
});
process.on('exit', function () {
mongoose.connection.close().then((res) => {
console.log('Mongo: Connection Closed.', res);
});
process.emit('cleanup');
});