forked from cmda-minor-web/progressive-web-apps-2122
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (70 loc) · 1.93 KB
/
app.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
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
const fetch = (...args) => import("node-fetch").then(({ default: fetch}) => fetch(...args))
const compression = require('compression');
// Set ejs in als template engine
app.set("view engine", "ejs");
app.set("views", "./views");
// Stel een static map in
app.use(compression());
app.use(express.static("public"));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
app.get("/", async (request, response) => {
const rijksAPI =
`https://www.rijksmuseum.nl/api/nl/collection?key=8Rynz75W&p=0-n&ps=10&imgonly=true`
const json = await fetch(rijksAPI)
.then(res => res.json())
.catch(e => {
console.error({
'message': 'oh no',
error: e,
})
})
// console.log(json.artObjects)
response.render("index", {
data: json.artObjects
})
});
app.get("/detail/:id", async (request, response) => {
const rijksAPI =
`https://www.rijksmuseum.nl/api/nl/collection/${request.params.id}?key=8Rynz75W&p=0-n&ps=10&imgonly=true`
const json = await fetch(rijksAPI)
.then(res => res.json())
.catch(e => {
console.error({
'message': 'oh no',
error: e,
})
}).then(data=>{
response.render("detail", {
data: data
})
})
});
app.get("/search", async (request, response) => {
const rijksAPI =
`https://www.rijksmuseum.nl/api/nl/collection?key=8Rynz75W&q=${request.query.q}&p=0-n&ps=10&imgonly=true`
console.log(request.query.q)
const json = await fetch(rijksAPI)
.then(res => res.json())
.catch(e => {
console.error({
'message': 'oh no',
error: e,
})
})
console.log(json.artObjects)
response.render("index", {
data: json.artObjects
})
});
app.get('/offline', function (request, response) {
response.render('fallback')
})
app.get("/detail", function (request, response) {
response.render('detail')
});
// Service Worker