-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 1.74 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
68
//module imports
const express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
cookieSession = require('cookie-session'),
passport = require('passport');
const app = express();
//file imports
const keys = require('./config/keys'),
config = require('./config');
//middlewares, env, dev tools
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000, //a month in milliseconds
keys: [keys.cookieKey],
})
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize()); //make sure passport.initilize and .session are ABOVE the routes.
app.use(passport.session());
app.use(express.static(config.www));
// Passport
//nothing is being imported, we just want the file parsed, so we don't need to assign it.
require('./models/User');
require('./models/Admins');
require('./models/Survey');
require('./services/passport');
require('./routes/authRoutes')(app); //immediately pass the app thru the import
require('./routes/billingRoutes')(app);
require('./routes/surveyRoutes')(app);
//production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
console.log(`Running on Production`);
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
//database
mongoose.connect(
keys.getDbConnectionString(),
{ useNewUrlParser: true },
err => {
if (err) {
throw err;
} else {
console.log(`Successfully connected to database.`);
}
}
);
app.listen(config.PORT, err => {
if (err) {
throw err;
} else {
var date = new Date();
date.setTime(Date.now());
console.log(`${date.toLocaleTimeString()}\nListening on port ${config.PORT}`);
}
});