this is my first question in this forum ever, so pls be kind to me if i did something wrong.
first i will show you my code:
const express = require('express');
const fs = require('fs');
const app = express();
const port = 8081;
const filename = __dirname + '/jokes.json';
app.use(express.json());
app.get('/jokes/:id', (req, res) => {
fs.readFile(filename,"utf8", (err,data)=>{
if(err){
res.send("Error:"+ err);
}else{
const jokes = JSON.parse(data)[req.params.id];
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(jokes));
}
});
});
the Jokes.json file looks like this:
{
"jokes" : [
{ "id": "1",
"title": "Zwei Bomben im Keller",
"text": "Sind zwei Bomben im Keller, sagt die eine zur anderen:\"Lass mal hochgehen.\" "
},
{ "id": "2",
"title": "Mönche in Asien",
"text": "Hoch in den japanischen Bergen fragt der Zen-Schüler seinen Meister: „Meister Aikodo, warum denken die Europäer, dass wir alle gleich aussehen? - Antwortet der Meister: „Ich bin nicht Meister Aikodo."
},
{ "id": "3",
"title": "Privatsphäre",
"text": "Natürlich müsste ich mal die Fenster putzen, aber Privatsphäre ist auch wichtig."
},
{ "id": "4",
"title": "Fieser Peter",
"text": "Susanne: Hallöchen! Peter: Hallöchen! - Susanne: Wie geht es dir! Peter: Wie geht es dir! - Susanne: Äffst du mich nach?! Peter: Äffst du mich nach?! - Susanne: Ich bin hässlich (grinst) Peter: Ja, das stimmt."
},
{ "id": "5",
"title": "Teewitz",
"text": "Welchen Tee sollte man besser nicht trinken? - TNT"
}]
}
So i'm trying to access a specific joke in my jokes array via the id component in the jokes array. But the problem is, when i start the server and i put "http://localhost:8081/jokes/1" in the URL, then nothing is shown at the website. So how can i do this, or is this even possible?
don't mind the Strings in the Jokes array, they are german, i'm german so my english is pretty weak, nonetheless i hope you can understand my problem/question and i'm thankfull for every hint/help you guys could give me :)
Edit: Thanks to you guys the Problem is solved (see below :) )