-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
76 lines (64 loc) · 2.05 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const cookieSession = require('cookie-session');
const passport = require('passport');
const path = require('path');
const authRoutes = require('./routes/authRoutes');
const userRoute = require('./routes/userRoute');
const profileRoutes = require('./routes/profileRoutes');
const courseRoute = require('./routes/courseRoute');
const flowRoute = require('./routes/flowRoute');
// Just want the passport setup to run ONCE
require('./controllers/passport-setup');
// set up enviromental variables
require('dotenv').config();
// Create app
const app = express();
// Preventing cors error
app.use(cors());
// set up session cookies (10 DAYS)
app.use(
cookieSession({
maxAge : 10 * 24 * 60 * 60 * 1000,
keys : [ process.env.COOKIE_KEY ]
})
);
// To Parse incoming requesrts with JSON payloads
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// initialize passport
app.use(passport.initialize());
app.use(passport.session());
// Connect to mongodb
const db = process.env.DATABASE.replace('<PASSWORD>', process.env.DATABASE_PASSWORD);
try {
mongoose.connect(db).then(() => {});
} catch (error) {
console.log('Cannot connect to DB', error);
}
// set up routes
app.use('/auth', authRoutes);
app.use('/user', userRoute);
app.use('/profile', profileRoutes);
app.use('/course', courseRoute);
app.use('/flow', flowRoute);
// Serve app if in production
if (process.env.NODE_ENV === 'production') {
//set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// create home route mainly for testing
app.get('/', (req, res) => {
if (req.user) {
res.json({ user: req.user });
} else {
const googleSignIn = "<a href='/auth/google'>Google</a>";
res.send(`Welcome to MadFlow, please sign in, or sign up: ${googleSignIn}`);
}
});
module.exports = app;