forked from fdnd-task/proof-of-concept
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
120 lines (103 loc) · 3.71 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
// 1. setup
// Import the npm package express from the node_modules folder
import express from 'express';
// Import the custom function fetchJson from the ./helpers folder
import fetchJson from './helpers/fetch-json.js';
// Set the base endpoint
const apiUrl = "https://fdnd-agency.directus.app/items/";
const apiHouse = `${apiUrl}f_houses/`;
const apiHouseIMG = `${apiUrl}f_houses?fields=*,poster_image.id,poster_image.height,poster_image.width/`;
const apiList = `${apiUrl}f_list/`;
const apiUsers = `${apiUrl}f_users/`;
const apiFeedback = `${apiUrl}f_feedback/`;
// Create a new express app
const app = express();
// Set ejs as the template engine
app.set('view engine', 'ejs');
app.set('views', './views');
// Use the 'public' folder for static resources, such as stylesheets, images, and client-side JavaScript
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
// 2. routes
app.get(["/", "/index"], async function (request, response) {
try {
const apiUsersData = await fetchJson(apiUsers);
response.render("index", {
apiUsers: apiUsersData.data
});
} catch (error) {
console.error("Error fetching data:", error);
response.status(500).send("Internal Server Error");
}
});
app.get("/lijsten", async function (request, response) {
try {
const housesPromise = fetchJson(apiHouse);
const listPromise = fetchJson(apiList);
const [houses, lists] = await Promise.all([housesPromise, listPromise]);
response.render("lijsten", {
houses: houses.data,
lists: lists.data
});
} catch (error) {
console.error("Error fetching data:", error);
response.status(500).send("Internal Server Error");
}
});
app.get('/lijsten/:id', function (request, response) {
fetchJson(apiList + request.params.id + '?fields=*.*.*,houses.f_houses_id.poster_image.id,houses.f_houses_id.poster_image.width,houses.f_houses_id.poster_image.height')
.then((apiData) => {
if (apiData.data.houses && apiData.data.houses.length > 0) {
response.render('lijst.ejs', { list: apiData.data });
} else {
response.render('lijst_empty.ejs');
}
}).catch((error) => {
console.error('Error fetching data:', error);
response.status(500).send('Internal Server Error');
});
});
app.post('/lijsten/:id', function(request, response) {
let body = JSON.stringify({
house: Number(request.body.house_id),
list: Number(request.body.list_id),
user: 11,
rating: JSON.stringify(request.body.rating),
note: request.body.notities
});
fetchJson(`https://fdnd-agency.directus.app/items/f_feedback/?limit=8000&filter[house][_eq]=${request.body.house_id}`, {
method: 'POST',
body: body,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
}).then((postResponse) => {
if (request.body.clientside) {
response.render('lijst', { added: true });
} else {
response.redirect(303, '/succes/');
}
}).catch(error => {
console.error('Fetch error:', error);
response.status(500).send('Internal Server Error');
});
});
app.get("/succes", async function (request, response) {
try {
const apiUsersData = await fetchJson(apiUsers);
response.render("succes", {
apiUsers: apiUsersData.data
});
} catch (error) {
console.error("Error fetching data:", error);
response.status(500).send("Internal Server Error");
}
});
// 3. Start the web server
// Set the port number for express to listen on
app.set('port', process.env.PORT || 8000);
// Start express, retrieve the port number
app.listen(app.get('port'), function () {
// Display a message in the console and provide the port number
console.log(`Application started on http://localhost:${app.get('port')}`);
});