-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
52 lines (42 loc) · 1.61 KB
/
app.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
const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('passport');
const flash = require('connect-flash');
const routes = require('./routes/index');
require('./helpers/passport');
// Create our Express app
const app = express();
// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Populates req.cookies with any cookies that came along with the request
app.use(cookieParser());
// Express Sessions allow us to store session data in MongoDB as opposed to cookies
app.use(session({
secret: process.env.SESSION_SECRET,
key: process.env.SESSION_KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
}));
// Initialize Passport & Passport Session middleware
app.use(passport.initialize());
app.use(passport.session());
// Flash middleware for server-side GUI flashes on response
app.use(flash());
// Inject our own routes
app.use('/', routes);
if (process.env.NODE_ENV === 'staging' || 'production') {
// Express will serve up client assets from the build folder
app.use(express.static('client/build'));
// Catch-all to serve up the the react-app to the client
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
module.exports = app;