-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
124 lines (97 loc) · 3.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Include modules
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
const handlebarHelpers = require('./handlebars-helpers')
const methodOverride = require('method-override')
const session = require('express-session')
const passport = require('passport')
const flash = require('connect-flash')
const csrf = require('csurf')
// check if it's in production mode
if (process.env.NODE_ENV !== 'production') { require('dotenv').config() }
// Initialize csrf protection middleware
const csrfProtection = csrf()
// Include controllers
const errorController = require('./controllers/error')
// Set up express-handlebars
app.engine('handlebars', exphbs({ default: 'main' }))
app.set('view engine', 'handlebars')
// Include routers
const homeRoutes = require('./routes/home')
const expenseRoutes = require('./routes/expense')
const userRoutes = require('./routes/user')
const searchRoutes = require('./routes/search')
const authRoutes = require('./routes/auths')
// Set up server related variable
const port = 3000
// connect to MongoDB, it will return a Connection object
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1/expense-tracker', {
useNewUrlParser: true, useCreateIndex: true
})
const db = mongoose.connection
// connection error
db.on('error', () => {
console.log('mongodb error!')
})
// connection success
db.once('open', () => {
console.log('mongodb connected!')
})
// Include Models
const User = require('./models/user')
const Record = require('./models/record')
// add middle-parser middleware
app.use(bodyParser.urlencoded({ extended: true }))
// set up session
app.use(session({
secret: 'joifhjoweifoikwenfokerjnofmweakmndlew',
resave: false,
saveUninitialized: false
}))
// use csrf protection middleware after session
app.use(csrfProtection)
// use flash middleware
app.use(flash())
// Initialize Passport
app.use(passport.initialize())
// use persistent login sessions
app.use(passport.session())
// include passport config
require('./config/passport')(passport)
// Set response local level variables to use in views during that cycle
app.use((req, res, next) => {
// safe user info
res.locals.user = req.user
// reminder message
res.locals.reminder = req.flash('reminder')
// error message
res.locals.error = req.flash('error')
// success message
res.locals.success = req.flash('success')
// generate one CSRF token to each render page
res.locals.csrfToken = req.csrfToken()
next()
})
// use method-override to override using a query value
app.use(methodOverride('_method'))
// serve static files
app.use(express.static('public'))
// home route
app.use('/', homeRoutes)
// expense routes
app.use('/expenses', expenseRoutes)
// search routes
app.use('/search', searchRoutes)
// user routes
app.use('/users', userRoutes)
// auth routes
app.use('/auth', authRoutes)
// error page
app.use(errorController.getError)
// Start and listen to server
app.listen(process.env.PORT || port, () => {
console.log(`Express is listening on http://localhost:${port}`)
})