generated from byui-wdd/340-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
112 lines (96 loc) · 3.15 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
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
/*******************************************
* This server.js file is the primary file of the
* application. It is used to control the project.
*******************************************/
/************************
* Require Statements
*************************/
const cookieParser = require("cookie-parser")
const session = require("express-session")
const pool = require("./database/")
const bodyParser = require("body-parser")
const express = require("express")
const expressLayouts = require("express-ejs-layouts")
const env = require("dotenv").config()
const app = express()
const static = require("./routes/static")
const baseController = require("./controllers/baseController")
const inventoryRoute = require("./routes/inventoryRoute")
const accountRoute = require("./routes/accountRoute")
const reviewRoute = require("./routes/reviewRoute") // Final Enhancement Task
const utilities = require("./utilities/")
/************************
* Middleware
*************************/
app.use(session({
store: new (require("connect-pg-simple")(session))({
createTableIfMissing: true,
pool,
}),
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
name: "sessionId",
}))
// Express Messages Middleware
app.use(require("connect-flash")())
app.use(function(req, res, next){
res.locals.messages = require("express-messages")(req, res)
next()
})
// Body parser Middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
// Cookie parser Middleware
// app.use(cookieParser()) // Original
app.use(cookieParser(process.env.SESSION_SECRET))
// JWT Middleware
app.use(utilities.checkJWTToken)
/************************
* View Engine and Templates
*************************/
app.set("view engine", "ejs")
app.use(expressLayouts)
app.set("layout", "./layouts/layout") // not at views root
/************************
* Routes
*************************/
app.use(static)
// Index route
app.get("/", utilities.handleErrors(baseController.buildHome))
// Inventory routes
app.use("/inv", inventoryRoute)
// Account routes
app.use("/account", accountRoute)
// review routes
app.use("/review", reviewRoute) // Final Enhancement Task
// File Not Found Route - must be last route in list
app.use(async (req, res, next) => {
next({status: 404, message: "Sorry, It looks like this road does not lead to Rome."})
})
/************************
* Express Error Handler
* Place after all other middleware
*************************/
app.use(async (err, req, res, next) => {
let nav = await utilities.getNav()
console.error(`Error at: "${req.originalUrl}": ${err.status}, ${err.message}`)
if(err.status == 404){ message = err.message} else {message = "Oh no! There was a crash. Maybe try a different route?"}
res.render("errors/error", {
title: err.status || "Server Error",
message,
nav
})
})
/************************
* Local Server Information
* Values from .env (environment) file
*************************/
const port = process.env.PORT
const host = process.env.HOST
/************************
* Log statement to confirm server operation
*************************/
app.listen(port, () => {
console.log(`app listening on ${host}:${port}`)
})