-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (50 loc) · 1.58 KB
/
index.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
const express = require('express');
const mongoose = require('mongoose');
//For accessing cookies for authentication
const cookieSession = require('cookie-session');
const passport = require('passport');
//For MongoURI and encrypting cookies in cookieSession
const keys = require('./config/keys');
// For user model class in MongoDB
require('./models/user');
// For survet model class in MongoDB
require('./models/survey');
// For including the authentication strategy
require('./services/passport');
// Estabilishing MongoDB connection
mongoose.connect(keys.mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Initialising express app
const app = express();
// Express body parser
app.use(express.json());
//Enabling cookies
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey],
})
);
app.use(passport.initialize());
app.use(passport.session());
// For handling authentication routes
require('./routes/authRoutes')(app);
// For handling billing
require('./routes/billingRoutes')(app);
// For handling survey routes
require('./routes/surveyRoutes')(app);
// Routing logic for production environment
if (process.env.NODE_ENV === 'production') {
// Serving production assests (production build of CRA)
app.use(express.static('client/build'));
// Serving index.html if route not recognised by express server
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// Listening on dynamic env port or 5000
const PORT = process.env.PORT || 5000;
app.listen(PORT);