-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.8 KB
/
index.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
const express = require('express')
const path = require('path')
const csrf = require('csurf')
const flash = require('connect-flash')
const mongoose = require('mongoose')
const exphbs = require('express-handlebars')
const session = require('express-session')
const MongoStore = require('connect-mongodb-session')(session)
const homeRoutes = require('./routes/home')
const cardRoutes = require('./routes/card')
const addRoutes = require('./routes/add')
const ordersRoutes = require('./routes/orders')
const coursesRoutes = require('./routes/courses')
const authRoutes = require('./routes/auth')
const varMiddleware = require('./middleware/variables')
const userMiddleware = require('./middleware/user')
const keys = require('./keys')
const PORT = process.env.PORT || 3000
const app = express()
const hbs = exphbs.create({
defaultLayout: 'main',
extname: 'hbs',
helpers: require('./utils/hbs-helpers')
})
const store = new MongoStore({
collection: 'sessions',
uri: keys.MONGODB_URI
})
app.engine('hbs', hbs.engine)
app.set('view engine', 'hbs')
app.set('views', 'views')
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.urlencoded({extended: true}))
app.use(session({
secret: keys.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store
}))
app.use(csrf())
app.use(flash())
app.use(varMiddleware)
app.use(userMiddleware)
app.use('/', homeRoutes)
app.use('/add', addRoutes)
app.use('/courses', coursesRoutes)
app.use('/card', cardRoutes)
app.use('/orders', ordersRoutes)
app.use('/auth', authRoutes)
async function start() {
try {
await mongoose.connect(keys.MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})
} catch (e) {
console.log(e)
}
}
start()