-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress.js
112 lines (96 loc) · 3.7 KB
/
express.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
const express = require("express");
const { read } = require("fs");
const app = express();
app.use(express.json());
rapsongs = [{name: "N95 KENDRICK LAMAR", year: 2021}, {name:"Dance Now JID", year: 2022}, {name:"In Da Club 50 CENT", year: 2003}];
rocksongs = [{name:"Bohemian Rhapsody QUEEN", year: 1975}, {name:"Holiday TURNSTILE", year: 2022}, {name:"Without Me DAYSEEKER", year: 2021}];
punksongs = [{name:"Troglodyte VIAGRA BOYS", year: 2021}, {name:"State of Mind STIFF RICHARDS", year: 2019}, {name:"Hell of Mine JOE UNKNOWN", year: 2022}]
popsongs = [{name:"Stay KID LAROI", year: 2021}, {name:"Viva la Vida COLDPLAY", year: 2008}, {name:"Goodbyes POST MALONE", year: 2019}];
indiesongs = [{name:"I Don't Wanna Party THE POLARITY", year: 2020}, {name:"how would you know SECOND THOUGHTS", year: 2021}, {name:"tell me a joke QUADECA", year: 2022}];
const genres = [
{id: 1, name:"Rap", songs: rapsongs},
{id: 2, name:"Rock", songs: rocksongs},
{id: 3, name:"Punk", songs: punksongs},
{id: 4, name:"Pop", songs: popsongs},
{id: 5, name: "Indie", songs: indiesongs}
]
//GET REQUESTS
//Welcome Page
app.get("/", (req, res)=> {
res.send("Welcome to the MUSIC APP");
})
//Genres Page
app.get('/genres', (req,res)=> {
res.send(genres);
})
//Genre ID Page
app.get('/genres/:id', (req,res)=> {
const genre = genres.find(c=> c.id === parseInt(req.params.id));
if(!genre) {
res.status(404).send("The genre with the given ID was not found");
return
}
res.send(genre);
})
//Song Page
app.get('/genres/:id/:year', (req, res)=> {
const genre = genres.find(c=> c.id === parseInt(req.params.id));
if(!genre) {
res.status(404).send("The genre with the given ID was not found");
return
}
const songs = genre.songs.find(c=> c.year === parseInt(req.params.year))
if (!songs) {
res.status(404).send("Songs with the given year was not found");
return
}
res.send(songs);
})
//POST REQUESTS
app.post("/genres", (req, res) => {
if (req.body.name.length > 2) {
const genre = {
id: genres.length + 1,
name: req.body.name
}
genres.push(genre);
res.send(genres);
}
else {
res.status(400).send("Name is required and with a minimum of 3 characters");
}
})
//PUT REQUESTS
app.put("/genres/:id", (req, res)=> {
const genre = genres.find(c=> c.id === parseInt(req.params.id));
if(!genre) {
res.status(404).send("The genre with the given ID was not found");
return
}
if (req.body.name.length > 2) {
genre.name = req.body.name;
genre.id = req.body.id
genres.splice(genre.id-1, 1, genre)
res.send(genres);
}
else {
res.status(400).send("Name is required and with a minimum of 3 characters");
}
})
//DELETE REQUESTS
app.delete("/genres/:id", (req, res) => {
const genre = genres.find(c=> c.id === parseInt(req.params.id));
if(!genre) {
res.status(404).send("The genre with the given ID was not found");
return
}
genres.splice(genres.indexOf(genre), 1)
res.send(genre);
})
app.listen(3000, () => {
console.log("Listening on port 3000 ...");
});
//REFLECTION
//Programs communicate with each other using requests and responses through servers. When a program requests data to a server, the server receives the requests and responds with the data for the program
//From this project, I learned that there are a lot of transactions and background action that I didn't realize websites and programs do every second
//This project can be further extended by creating more features and filters that better enhance the user and program experience.