import express from "express";
import bodyParser from "body-parser";
const app = express();
const port = 3000;
const masterKey = "4VGP2DN-6EWM4SJ-N6FGRHV-Z3PR3TT";
app.use(bodyParser.urlencoded({ extended: true }));
//1. GET a random joke
app.get("/random", (req, res) => {
const idx = Math.floor(Math.random * jokes.length);
res.json(jokes[idx]);
})
//2. GET a specific joke
// app.get("/joke", (req, res) => {
// const id = parseInt(req.body.id);
// res.json(jokes[id]);
// })
app.get("/joke/:id", (req, res) => {
const id = parseInt(req.params.id);
const foundJoke = jokes.find((joke) => joke.id === id);
res.json(foundJoke);
})
/*
params는 URL 경로에 있는 변수들을 의미한다.
jokes가 find 함수를 지원하는 건 jokes가 배열이기 때문,
배열은 자바스크립트에서 기본적으로 제공하는 객체로,
배열에는 find 메소드가 있다.
*/
//3. GET a jokes by filtering on the joke type
// app.get("/filter", (req, res) => {
// const type = req.query.type;
// var output = [];
// jokes.forEach((ele) => {
// if(ele.jokeType === type) {
// output.push(ele);
// }
// })
// res.json(output);
// })
app.get("/filter", (req, res) => {
const type = req.query.type;
const filterdActivities = jokes.filter((joke) => joke.jokeType === type);
res.json(filteredActivities);
} )
//4. POST a new joke
app.post("/jokes", (req, res) => {
const newJoke = {
id: jokes.length +1,
// jokeText: req.body.jokeText,
// jokeType: req.body.jokeType
jokeText: req.body.text,
jokeType: req.body.type,
};
jokes.push(newJoke);
// console.log(jokes[-1]);
console.log(jokes.slice(-1));
res.json(jokes);
})
//5. PUT a joke
app.put("/jokes:id", (req, res) => {
const curId = parseInt(req.params.id);
const newJoke = {
id: curId,
jokeText: req.body.text,
jokeType: req.body.type,
};
// for(const eachJoke of jokes) {
// if(eachJoke.id === curId) {
// jokes[eachJoke.id] = newJoke;
// console.log(jokes[eachJoke.id]);
// res.json(newJoke);
// break;
// }
// }
const searchIndex = jokes.findIndex((joke) => joke.id === curId);
jokes[searchIndex] = newJoke;
res.json(newJoke);
})
//6. PATCH a joke
app.patch("/jokes:id", (req, res) => {
const curId = parseInt(req.params.id);
// if(req.body.text) {
// newText: req.body.text;
// } else {
// newText: jokes[curId].jokeText;
// }
// if(req.body.type) {
// newType:req.body.type;
// } else {
// newType: jokes[curId].jokeType;
// }
// const newJoke = {
// id: curId,
// jokeText: newText,
// jokeType, newType,
// };
// jokes[curId] = newJoke;
// res.json(newJoke);
const searchIndex = jokes.findIndex((joke) => joke.id === id);
const newJoke = {
id: curId,
jokeText: req.body.text || jokes[searchIndex].jokeText,
jokeType: req.body.type || jokes[searchindex].jokeType,
};
jokes[searchIndex] = newJoke;
console.log(jokes[searchIndex]);
res.json(newJoke);
});
//7. DELETE Specific joke
app.delete("/jokes:id", (req, res) => {
const curId = parseInt(req.params.id);
const searchIdx = jokes.findIndex((joke) => joke.id === curId);
if(searchIdx > -1) {
jokes.splice(searchIdx, 1) ;
res.sendStatus(200);
} else {
res
.status(404)
.json({error: `Joke with id: ${curId} not found.
No jokes were deleted.`});
}
// 이러면 id가 하나 비어있게 되지 않나?
})
//8. DELETE All jokes
app.delete("/all", (req, res) => {
const userKey = req.query.key;
// if(req.body.key === masterKey) {
if(userKey === masterKey) {
jokes = [];
console.log('OK');
res.sendStatus(200);
} else {
res
.status(404)
.json({error: 'You are not authorised to perform this action.'});
// .json({error: 'If you want delete all, You have to get masterKey'});
}
})
app.listen(port, () => {
console.log(`Successfully started server on port ${port}.`);
});
var jokes = [
{
id: 1,
jokeText:
"Why don't scientists trust atoms? Because they make up everything.",
jokeType: "Science",
},
{
id: 2,
jokeText:
"Why did the scarecrow win an award? Because he was outstanding in his field.",
jokeType: "Puns",
},
{
id: 3,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 4,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 5,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 6,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 7,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 8,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 9,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 10,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 11,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 12,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 13,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 14,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 15,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 16,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 17,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 18,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 19,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 20,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 21,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 22,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 23,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 24,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 25,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 26,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 27,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 28,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 29,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 30,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 31,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 32,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 33,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 34,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 35,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 36,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 37,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 38,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 39,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 40,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 41,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 42,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 43,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 44,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 45,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 46,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 47,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 48,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 49,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 50,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 51,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 52,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 53,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 54,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 55,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 56,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 57,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 58,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 59,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 60,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 61,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 62,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 63,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 64,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 65,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 66,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 67,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 68,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 69,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 70,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 71,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 72,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 73,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 74,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 75,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 76,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 77,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 78,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 79,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 80,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 81,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 82,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 83,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 84,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 85,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 86,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 87,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
{
id: 88,
jokeText: "Why did the tomato turn red? Because it saw the salad dressing!",
jokeType: "Food",
},
{
id: 89,
jokeText:
"What do you get when you cross a snowman and a vampire? Frostbite!",
jokeType: "Wordplay",
},
{
id: 90,
jokeText:
"Why did the golfer bring two pairs of pants? In case he got a hole in one!",
jokeType: "Sports",
},
{
id: 91,
jokeText:
"Why are ghosts bad at lying? Because you can see right through them!",
jokeType: "Wordplay",
},
{
id: 92,
jokeText: "Why can't you give Elsa a balloon? Because she will let it go.",
jokeType: "Movies",
},
{
id: 93,
jokeText:
"I'm reading a book about anti-gravity. It's impossible to put down!",
jokeType: "Science",
},
{
id: 94,
jokeText:
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
jokeType: "Puns",
},
{
id: 95,
jokeText:
"What did one ocean say to the other ocean? Nothing, they just waved.",
jokeType: "Wordplay",
},
{
id: 96,
jokeText:
"Why do we never tell secrets on a farm? Because the potatoes have eyes and the corn has ears.",
jokeType: "Wordplay",
},
{
id: 97,
jokeText: "How do you organize a space party? You planet!",
jokeType: "Science",
},
{
id: 98,
jokeText:
"Why don't some couples go to the gym? Because some relationships don't work out.",
jokeType: "Puns",
},
{
id: 99,
jokeText:
"Parallel lines have so much in common. It's a shame they'll never meet.",
jokeType: "Math",
},
{
id: 100,
jokeText: "What do you call fake spaghetti? An impasta!",
jokeType: "Food",
},
];