-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (46 loc) · 1.17 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
require("dotenv").config()
const express = require("express")
const server = express()
const path = require("path")
const port = process.env.PORT || 3000
const paintings = require("./paintings.js")
let start
// Middleware
server.use(express.urlencoded({ extended: false }))
server.use(express.static(path.join(__dirname, "public")))
server.use(express.json())
server.set("view engine", "ejs")
server.set("views", path.join(__dirname, "views"))
// Routes
server.get("/", (req, res) => {
start = false
res.render("home", {
paintings: paintings,
start: start
})
})
server.get("/paintings/:id", (req, res, next) => {
let index = parseInt(req.params.id)
start = true
if (isNaN(index)) {
next(new Error("Test error"))
return
}
res.render("slideshow", {
paintings: paintings,
index: index,
start: start
})
})
// Handle 404 errors (Page Not Found)
server.use((req, res) => {
res.status(404).send("Page Not Found")
})
// Error handling middleware
server.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send("Internal Server Error")
})
server.listen(port, () => {
console.log(`Server listening on port ${port}.`)
})