forked from fdnd-task/enhanced-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
196 lines (157 loc) · 6.73 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Importeer het npm pakket express uit de node_modules map
import express from 'express'
// Importeer de zelfgemaakte functie fetchJson uit de ./helpers map
import fetchJson from './helpers/fetch-json.js'
// Stel het basis endpoint in
const apiUrl = 'https://redpers.nl/wp-json/wp/v2'
// Directus link
const apiDirectus = 'https://fdnd-agency.directus.app/items/redpers_shares'
// https://github.com/ju5tu5/the-web-is-for-everyone-interactive-functionality
//Methode: post
//Params --> aangevraagde slug
//Gebruik een form voor de post data
// Haal alle variabelen op die je wil maken.
const apiPosts = apiUrl + '/posts'
const apiUsers = apiUrl + '/users'
const apiCategories = apiUrl + '/categories'
// Maak een nieuwe express app aan
const app = express()
// Stel ejs in als template engine
app.set('view engine', 'ejs')
// Stel de map met ejs templates in
app.set('views', './views')
// Gebruik de map 'public' voor statische resources, zoals stylesheets, afbeeldingen en client-side JavaScript
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
// Maak een GET route voor de index (home)
app.get('/', function (_request, response) {
// Haal alle personen uit de WHOIS API op
fetchJson(apiPosts).then((apiData) => {
// apiData bevat gegevens van alle personen uit alle squads
// Je zou dat hier kunnen filteren, sorteren, of zelfs aanpassen, voordat je het doorgeeft aan de view
// Render index.ejs uit de views map en geef de opgehaalde data mee als variabele, genaamd persons
response.render('home.ejs', {articles: apiData,messages:messages})
})
})
const messages=[]
// Maak een POST route voor de index (home)
app.post('/', function (request, response) {
messages.push(request.body.messages)
console.log(JSON.stringify(messages))
// Er is nog geen afhandeling van POST, redirect naar GET op /
response.redirect(303, '/')
})
// import express from 'express'
// import fetchJson from './helpers/fetch-json.js'
// // It's an array of objects containing slug and like#, eg. [{ slug: 'a', likes: 42}]
// let likes = []
// const redpers_url = 'https://redpers.nl/wp-json/wp/v2/posts'
// const directus_url = 'https://fdnd-agency.directus.app/items/redpers_shares'
// const app = express()
// app.set('view engine', 'ejs')
// @@ -12,37 +12,47 @@ app.use(express.static('public'))
// app.use(express.urlencoded({ extended: true }))
// app.get('/', (request, response) => {
// fetchJson('https://redpers.nl/wp-json/wp/v2/posts').then((data) => {
// fetchJson('https://fdnd-agency.directus.app/items/redpers_shares').then((share_data) => {
// response.render('index', { articles: data, shares: share_data.data })
// fetchJson(redpers_url).then((data) => {
// fetchJson(directus_url).then((shares) => {
// // Map de data array uit wordpress, map is een soort 'loop' structuur voor arrays
// data.map((article) =>
// // Gebruik de Object.assign() functie om het aantal shares toe te voegen op het article
// // NB: Object.assign past het 'article' object
// Object.assign(article, {
// // Zoek in shares naar article.slug, koppel het aantal shares of 0 als het niet bestaat
// shares: shares.data.find(({ slug }) => slug == article.slug)?.shares || 0,
// })
// )
// response.render('index', { articles: data })
// })
// })
// })
// app.post('/article/:slug', (request, response) => {
// // Zoek in de array de likes voor het huidige artikel
// let huidig = likes.find((like) => {
// return like.slug == request.params.slug
// })
// // Failsafe voor als iemand /article intypt zonder slug...
// app.get('/article', (request, response) => {
// response.redirect(301, '/')
// })
// // Als het huidige artikel nog niet bestaat maken we likes aan
// if (huidig == undefined) {
// likes.push({
// slug: request.params.slug,
// likes: 1,
// app.post('/article/:slug', (request, response) => {
// fetchJson(`${directus_url}?filter[slug][_eq]=${request.params.slug}`).then(({ data }) => {
// // Doe een PATCH op directus, stuur de id mee als die er is.
// fetchJson(`${directus_url}/${data[0]?.id ? data[0].id : ''}`, {
// method: data[0]?.id ? 'PATCH' : 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({
// slug: request.params.slug,
// shares: data.length > 0 ? data[0].shares + 1 : 1,
// }),
// }).then((result) => {
// console.log(result)
// })
// // Als het al wel bestaat tellen we er een like bij op
// } else {
// huidig.likes++
// }
// })
// response.redirect(301, `/article/${request.params.slug}`)
// })
// app.get('/article/:slug', (request, response) => {
// fetchJson(`https://redpers.nl/wp-json/wp/v2/posts/?slug=${request.params.slug}`).then((data) => {
// // Zoek naar de likes voor deze slug en voeg ze toe aan de opgehaalde API data
// let current_likes = likes.find((like) => like.slug == request.params.slug)
// data[0].likes = current_likes?.likes || 0
// fetchJson(`${redpers_url}?slug=${request.params.slug}`).then((data) => {
// // fetch de #shares
// response.render('article', { article: data[0] })
// })
// })
app.get('/artikel/:slug', function (request, response) {
// --> dit zijn routes (home, article, category)
// Hier haal je de url op en maak je er een Json file van ipv een link. Waarna het wordt vernoemd naar apiData
fetchJson(apiPosts + '?slug=' + request.params.slug).then((apiData) => {
// Deze info wordt daarna
// meegegeven aan de toegewezen EJS
response.render('article.ejs', {article: apiData[0]
// .data is belangrijk om er bij te schrijven
// alle id's zijn een soort van mappen, en door .data te schrijven ga je eigenlijk een map 'dieper'
})
// console.log(apiData)
})
})
// Stap 4 | Het maken van categorieeen in routes
const categoryRoutes = [
'binnenland',
'buitenland',
'column',
'economie',
'kunst & media',
'podcast',
'politiek',
'wetenschap'
]
// https://redpers.nl/wp-json/wp/v2/posts?filter=[articleSection]=%22binnenland%22
categoryRoutes.forEach(category => {
app.get("/" + category, function (request, response) {
fetchJson(apiCategories + "?filter=" + category).then((categoryApi) => {
// Kijken of alle goede dat is binnengekomen
// console.log(categoryApi)
response.render('category.ejs')
});
});
})
// Stel het poortnummer in waar express op moet gaan luisteren
app.set('port', process.env.PORT || 1000)
// Start express op, haal daarbij het zojuist ingestelde poortnummer op
app.listen(app.get('port'), function () {
// Toon een bericht in de console en geef het poortnummer door
console.log(`Application started on http://localhost:${app.get('port')}`)
})