-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (44 loc) · 1.6 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
// Initialize the express serving library
const express = require('express')
// express sessiosn library for login things
const session = require('express-session')
// mongo package to save sessions to database
const MongoStore = require('connect-mongo')(session)
// flash messaging package
const flash = require('connect-flash')
// initizlie the app
const app = express()
// set up the session options
let sessionOptions = session({
secret: "Secret key is hard to guess when you do this!@#$fa$%Gaf211",
store: new MongoStore({client: require('./db')}),
resave: false,
saveUninitialized: false,
cookie: {maxAge: 1000 * 60 * 60 * 24,
httpOnly: true}
})
app.use(sessionOptions)
app.use(flash())
// set it up to pass local user data to each session?
// must be before the router, so it gets the user data before the route is passed
app.use(function(req, res, next) {
// make current user id available on the request object
if (req.session.user) {req.visitorId = req.session.user._id} else {req.visitorId = 0}
// make user sesion data avilable from within view templates
res.locals.user = req.session.user
next()
})
const router = require('./router')
// set up express to pass form values
app.use(express.urlencoded({extended: false}))
// tell app to accept json data
app.use(express.json())
// Set the app to serve our public folder
app.use(express.static('public'))
// Set the folder for the views
app.set('views', 'views')
// set the javascript view rendering engine
app.set('view engine', 'ejs')
// set up the router from import
app.use('/', router)
module.exports = app