-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (64 loc) · 1.88 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
if (process.env.NODE_ENV == 'production') {
console.log("STARTING PRODUCTION ENVIRONMENT")
} else {
console.log("STARTING DEVELOPMENT ENVIRONMENT")
// require('dotenv').config()
}
// Loading environmental variables here
require('dotenv').config()
const routes = require('./routes')
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
const dbConnection = require('./db') // loads our connection to the mongo database
const passport = require('./passport')
const app = express()
const PORT = process.env.PORT || 3001
// ===== Middleware ====
app.use(morgan('dev'))
app.use(
bodyParser.urlencoded({
extended: false
})
)
app.use(bodyParser.json())
app.use(
session({
secret: process.env.APP_SECRET || 'this is the default passphrase',
store: new MongoStore({ mongooseConnection: dbConnection }),
resave: false,
saveUninitialized: false
})
)
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// ===== Passport ====
app.use(passport.initialize())
app.use(passport.session()) // will call the deserializeUser
// ===== testing middleware =====
app.use(function(req, res, next) {
console.log('===== Session =======')
console.log(req.session)
// console.log(req.user)
console.log('===== END =======')
next()
})
/* Express app authentication ROUTING */
app.use('/auth', require('./auth'))
// ====== Error handler ====
app.use(function(err, req, res, next) {
console.log('====== ERROR =======')
console.error(err.stack)
res.status(500)
})
// Add router for all other routes
app.use(routes);
// ==== Starting Server =====
app.listen(PORT, () => {
console.log(`App listening on PORT: ${PORT}`)
})