forked from fdnd-task/pleasurable-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
173 lines (144 loc) · 6.41 KB
/
server.js
File metadata and controls
173 lines (144 loc) · 6.41 KB
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
/*** Express setup & start ***/
import fetchJson from './helpers/fetch-json.js'
import express, { request, response } from 'express'
const app = express()
app.set('view engine', 'ejs')
app.set('views', './views')
app.use(express.static('public'))
app.use(express.urlencoded({extended: true}))
app.set('port', process.env.PORT || 8000)
app.listen(app.get('port'), function () {
console.log(`Application started on http://localhost:${app.get('port')}`)
})
/*** Variabelen ***/
const redpersUrl = 'https://redpers.nl/wp-json/wp/v2/',
directusUrl = 'https://fdnd-agency.directus.app/items/redpers_shares',
postsUrl = redpersUrl + 'posts',
categoriesUrl = redpersUrl + 'categories',
authorUrl = redpersUrl + 'users',
categoriesData = [
{"id": 9, "name": "Binnenland", "slug": "binnenland"},
{"id": 1010, "name": "Buitenland", "slug": "buitenland"},
{"id": 7164, "name": "Column", "slug": "column"},
{"id": 6, "name": "Economie", "slug": "economie"},
{"id": 4, "name": "Kunst & Media", "slug": "kunst-media"},
{"id": 3211, "name": "Podcasts", "slug": "podcast"},
{"id": 63, "name": "Politiek", "slug": "politiek"},
{"id": 94, "name": "Wetenschap", "slug": "wetenschap"},
];
// Datum
const date = new Map();
const getDate = `day = parsedDate.getDate(),
short = {month: "short"},
long = {month: "long"},
monthShort = Intl.DateTimeFormat("nl-NL", short).format(parsedDate),
monthLong = Intl.DateTimeFormat("nl-NL", long).format(parsedDate),
year = parsedDate.getFullYear(),
hours = (parsedDate.getHours() < 10 ? '0' : ' ') + parsedDate.getHours(),
minutes = (parsedDate.getMinutes() < 10 ? '0' : '') + parsedDate.getMinutes(),
time = hours + ':' + minutes,
dayMonth = day + ' ' + monthShort,
dayMonthYear = day + ' ' + monthLong + ' ' + year,
fullDate = day + ' ' + monthLong + ' ' + year + ', ' + time;`
date.set('day-month', `const parsedDate = new Date(postData[i].date),
${getDate}
postData[i].date = dayMonth`);
date.set('day-month-year', `const parsedDate = new Date(postData[i].date),
${getDate}
postData[i].date = dayMonthYear`)
date.set('full-date', `const parsedDate = new Date(postData[0].date),
${getDate}
postData[0].date = fullDate`)
/*** Routes & data ***/
//Index route
app.get('/', (request, response) => {
Promise.all([
Promise.all(categoriesData.map(category =>
fetchJson(`${postsUrl}?per_page=3&categories=${category.id}`)
)),
fetchJson(`${postsUrl}?per_page=4`)
]).then(([postData, featuredData]) => {
response.render('index', { categories: categoriesData, posts: postData, featured: featuredData });
})
})
//Artikel route
app.get("/artikel/:slug", (request, response) => {
const slugdirectus = encodeURIComponent(request.params.slug)
Promise.all([
fetchJson(`${postsUrl}/?slug=${request.params.slug}&_fields=date,slug,title,author,content,excerpt,categories,yoast_head,yoast_head_json`),
fetchJson(`${directusUrl}?filter={"slug":"${slugdirectus}"}`),
fetchJson(`${authorUrl}?_fields=id,slug,name,description,avatar_urls&per_page=100`),
fetchJson(`${categoriesUrl}?_fields=id,name,slug&per_page=100`)
]).then(([postData, likeData, authorData, categoryData]) => {
let filterCategorie = categoryData.filter(category => {
return category.id == postData[0].categories[0]
})
let filterAuthor = authorData.filter(author =>{
return author.id == postData[0].author
})
eval(date.get('full-date'))
if (postData[0].categories.some(category => category === 590)){
response.render("gallery", {article: postData, like: likeData.data, categories: categoriesData, category: filterCategorie, author: filterAuthor})
} else if (postData[0].categories.some(category => category === 3211)){
response.render("podcast", {article: postData, like: likeData.data, categories: categoriesData, category: filterCategorie, author: filterAuthor})
} else {
response.render("article", {article: postData, like: likeData.data, categories: categoriesData, category: filterCategorie, author: filterAuthor})
}
})
})
app.post('/artikel/:slug', (request, response) => {
fetchJson(`${directusUrl}?filter[slug][_eq]=${request.params.slug}`)
.then(({ data }) => {
return fetchJson(`${directusUrl}/${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(() => {
response.redirect(301, `/artikel/${request.params.slug}`);
})
.catch(error => {
console.error('Error:', error);
response.status(500).send('Internal Server Error');
});
});
//Categorie route
app.get('/categorie/:slug', function (request, response) {
const category = categoriesData.find((category) => category.slug == request.params.slug)
Promise.all([
fetchJson(`${postsUrl}?categories=${category.id}&_fields=date,slug,title,yoast_head_json.og_image, yoast_head_json.og_image,jetpack_featured_media_url&per_page=20`),
fetchJson(`${categoriesUrl}/?slug=${request.params.slug}&_fields=name,yoast_head`)
]).then(([postData, category]) => {
for (var i=0; i < postData.length; i++) {
eval(date.get('day-month-year'))
}
response.render('category', {posts: postData, category: category, categories: categoriesData});
})
})
//Auteur route
app.get('/auteur/:slug', function (request, response) {
Promise.all([fetchJson(authorUrl + '?slug=' + request.params.slug),
fetchJson(postsUrl + '?_fields=date,slug,title,author,yoast_head_json.twitter_misc,yoast_head_json.og_image,jetpack_featured_media_url&per_page=100')]).then(([authorData, postData]) => {
let filterPost = postData.filter(post =>{
return post.author == authorData[0].id
})
for (var i=0; i < postData.length; i++) {
eval(date.get('day-month'))
}
response.render('author', {author: authorData, posts: filterPost, categories: categoriesData })
})
})
// Search
app.get('/search', (request, response) => {
const searchterm = request.query.q
fetchJson(`${postsUrl}?search=${searchterm}`).then((postData) => {
for (var i=0; i < postData.length; i++) {
eval(date.get('day-month-year'))
}
response.render('search', {posts: postData, categories: categoriesData, searchterm})
})
})