-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
110 lines (89 loc) · 2.56 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
const express = require("express")
const app = express()
const PORT = 8080
const path = require("path")
const modelService = require("./moduleService")
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// modelService.testFx()
// let checkModelID = (req, res, next) => {
// if (req.params.id) {
// if (isNaN(req.params.id)) {
// res.status(400).send("ID must be a number")
// return
// }
// }
// next()
// }
app.use(express.static(__dirname + '/public'));
app.get("/",(req, res) => {
// res.sendFile(path.join(__dirname, "/views/index.html"))
modelService.getModels().then((models) => {
res.render("index", {
models: models
})
})
})
app.get("/about", (req, res) => {
// res.sendFile(path.join(__dirname, "/views/about.html"))
res.render("about")
})
app.get("/models", (req, res) => {
// conditional rendering of req.query.id
// /models?id=1
if (req.query.category) {
modelService.getModelByCategory(req.query.category).then((modelsByCategory) => {
res.render("index", {
models: modelsByCategory
})
}).catch((err) => {
console.log(err)
})
} else {
modelService.getModels().then((models) => {
res.json(models)
}).catch((err) => {
console.log(err)
})
}
})
app.get("/categories", (req, res) => {
modelService.getCategories().then((categories) => {
res.render("categories", {
categories: categories
})
}).catch((err) => {
console.log(err)
})
})
// app.get( FIRST ARGUMENT: PATH/NAME OF THE ROUTE, OPTIONAL: MIDDLEWARE, OPTIONAL: MIDDLEWARE, OPTIONAL: MIDDLEWARE, ,SECOND ARGUMENT: CALLBACK (req, res) => {
// res.send etc etc
//})
// variable route with req.params.id
app.get("/models/:id",(req, res) => {
modelService.getModelByID(req.params.id).then((modelData) => {
res.send(modelData)
}).catch((err) => {
console.log(err)
})
})
app.get("/test", (req, res) => {
let header = req.get("accept-language")
res.send(header)
})
app.get("/test/:id", (req, res) => {
res.send(`<h1>your id: ${req.params.id}</h1>`)
})
// app.get("/models/test", (req, res) => {
// res.send("test")
// })
app.get("*", (req, res) => {
res.send("<h1>404 THIS ROUTE DOES NOT EXIST</h1>")
})
modelService.initialize().then(() => {
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`)
})
}).catch((err) => {
console.log(err)
})