-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (52 loc) · 2.91 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
// Import Mongoose package, then keys config and our model class files.
// It is essential that User model be imported before /services/passport.js references it.
const mongoose = require('mongoose');
const keys = require('./config/keys');
require('./models/User');
require('./models/Survey');
// Import Express package and Body Parser Express middleware.
const express = require('express');
const bodyParser = require('body-parser');
// Import cookie-session package so we can later tell Passport to use it.
// Then import our local /services/passport.js config file which references Passport package.
const cookieSession = require('cookie-session');
const passport = require('passport');
require('./services/passport');
// Connect to MongoDB.
mongoose.connect(keys.mongoURI, { useNewUrlParser: true });
// Start Express server.
const app = express();
// app.use() -- Define Express middleware to handle some work before passing to route handlers.
app.use(bodyParser.json());
app.use(
cookieSession({
maxAge: 30 * 24 * 60 *60 * 1000, // Pass 30 days in milliseconds.
keys: [keys.cookieKey] // Array of strings to use in encrypting cookie, one will be chosen at random. We are only supplying one.
})
);
// Tell Passport to use cookie authentication.
app.use(passport.initialize());
app.use(passport.session());
// app.get(), app.post(), etc. -- Invoke route handlers.
// Import exported route handler function from authRoutes.js and invoke it passing app object. This code invokes Passport.
require('./routes/authRoutes')(app);
// Import exported route handler function from billingRoutes.js and invoke it passing app object. This invokes Stripe
// but before doing so, it runs middleware from server/middleware/requireLogin.js to ensure user is logged in.
require('./routes/billingRoutes')(app);
// Import exported route handler function from surveyRoutes.js and invoke it passing app object. This invokes ????
// but before doing so, it runs middleware from server/middleware/requireLogin.js to ensure user is logged in and
// requireCredits.js to ensure user has ability to pay for a new survey.
require('./routes/surveyRoutes')(app);
// Help Express in production locate routes in the client app when route handlers are not defined for them in the Express app.
if (process.env.NODE_ENV === 'production') {
// If Express receives a route that precisely matches a file inside the client/build/ path such as static/js/main.js or static/css/main.css...
app.use(express.static('client/build'));
// Otherwise, Express will serve up client/build/index.html if it does not recognize the route.
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// Express tells Node what port to listen on. We get it dynamically from Heroku if running in production.
const PORT = process.env.PORT || 5000;
app.listen(PORT);