-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (59 loc) · 1.7 KB
/
Copy pathapp.js
File metadata and controls
70 lines (59 loc) · 1.7 KB
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
const express = require('express')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const session = require('express-session')
const flash = require('connect-flash')
const usePassport = require('./config/passport')
const routes = require('./routes')
require('./config/mongoose')
if(process.env.NODE_ENV !== 'production'){
require('dotenv').config()
}
const app = express()
const PORT = process.env.PORT || 3000
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
helpers:{
fomattedDate: function(date) {
const dateObj = new Date(date)
const year = dateObj.getFullYear()
const month = String(dateObj.getMonth() + 1).padStart(2, "0")
const day = String(dateObj.getDate()).padStart(2, "0")
return `${year}/${month}/${day}`
},
cutDate: function(date) {
if (!date) return ""
return date.toISOString().substring(0, 10)
},
eq: function(a, b) {
return a == b
},
getImg: function(obj, categoryId) {
return obj[categoryId]
}
}
}))
app.set('view engine', 'hbs')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(methodOverride('_method'))
app.use(session({
secret: 'ThisIsMySecret',
resave: false,
saveUninitialized: true
}))
app.use(flash())
usePassport(app)
app.use((req, res, next) => {
res.locals.isAuthenticated = req.isAuthenticated()
res.locals.user = req.user
res.locals.success_msg = req.flash('success_msg')
res.locals.warning_msg = req.flash('warning_msg')
next()
})
app.use(routes)
app.listen(PORT, () => {
console.log(`App is running on http://localhost:${PORT}`)
})