forked from cmda-minor-web/progressive-web-apps-2122
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (65 loc) · 1.94 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
const express = require('express')
const fetch = require('node-fetch')
const app = express()
const compression = require('compression')
const port = process.env.PORT || 3000
require('dotenv').config({path: '.env-dev'})
const {
API_KEY
} = process.env
// Link the templating engine to the express app
app.set('view engine', 'ejs')
// Tell the views engine/ejs where the template files are stored (Settingname, value)
app.set('views', 'views')
app.use(/.*-[0-9a-f]{10}\..*/, (req, res, next) => {
res.setHeader('Cache-Control', 'max-age=365000000, immutable')
next()
})
// Tell express to use a 'static' folder
app.use(express.static('static'))
app.use(compression())
// Create a home route
app.get('/', (req, res) => {
fetch(`https://www.rijksmuseum.nl/api/nl/collection?key=${API_KEY}&ps=6`)
.then(async response => {
const artWorks = await response.json()
res.render('index', {
title: 'Art Museum',
data: artWorks.artObjects,
})
})
.catch(err => res.send(err))
})
// Detail page
app.get('/art/:id', function(req, res) {
fetch(`https://www.rijksmuseum.nl/api/nl/collection/${req.params.id}?key=${API_KEY}`)
.then(async response => {
const artWorks = await response.json()
res.render('detail', {
title: 'Art Museum' + req.params.id,
data: artWorks.artObject,
});
})
.catch(err => res.send(err))
})
// Search action
app.get('/search', (req, res) => {
fetch(`https://www.rijksmuseum.nl/api/nl/collection?key=${API_KEY}&q=${req.query.query}&imgonly=true`)
.then(async response => {
const artWorks = await response.json()
res.render('index', {
title: 'Art Museum',
data: artWorks.artObjects,
});
})
.catch(err => res.send(err))
})
// Offline page
app.get('/offline', (req, res) => {
res.render("offline", {
title: "You are Offline"
});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})