generated from blainerobertson/cse340starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (61 loc) · 2.06 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
/* ******************************************
* This is the application server
* ******************************************/
const express = require("express")
const app = express()
/* ***********************
* Require Statements
*************************/
const expressLayouts = require("express-ejs-layouts")
const env = require("dotenv").config()
const static = require("./routes/static")
const baseController = require("./controllers/baseController")
const inventoryRoute = require("./routes/inventoryRoute")
const utilities = require("./utilities/")
/* ***********************
* View Engine and Templates
*************************/
app.set("view engine", "ejs")
app.use(expressLayouts)
app.set("layout", "./layouts/layout") // not at views root
/* ***********************
* Static Files Middleware
*************************/
// Serve static files from the public folder
app.use(express.static("public"))
/* ***********************
* Routes
*************************/
app.use(static)
// Index route
app.get("/", utilities.handleErrors(baseController.buildHome))
app.use("/inv", inventoryRoute)
// File Not Found Route - must be last route in list
app.use(async (req, res, next) => {
next({status: 404, message: 'Sorry, we appear to have lost that page.'})
})
/* ***********************
* 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.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
})
})
/* ******************************************
* Server host name and port
* ***************************************** */
const HOST = 'localhost'
const PORT = 5500
/* ***********************
* Log statement to confirm server operation
* *********************** */
app.listen(PORT, () => {
console.log(`trial app listening on ${HOST}:${PORT}`)
})