-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (72 loc) · 2.34 KB
/
Copy pathserver.js
File metadata and controls
82 lines (72 loc) · 2.34 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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
// before our routes definition
app.use(bodyParser.json());
app.use(cors());
const albumsData = [
{
albumId: "10",
artistName: "Beyoncé",
collectionName: "Lemonade",
artworkUrl100:
"http://is1.mzstatic.com/image/thumb/Music20/v4/23/c1/9e/23c19e53-783f-ae47-7212-03cc9998bd84/source/100x100bb.jpg",
releaseDate: "2016-04-25T07:00:00Z",
primaryGenreName: "Pop",
url:
"https://www.youtube.com/embed/PeonBmeFR8o?rel=0&controls=0&showinfo=0"
},
{
albumId: "11",
artistName: "Beyoncé",
collectionName: "Dangerously In Love",
artworkUrl100:
"http://is1.mzstatic.com/image/thumb/Music/v4/18/93/6d/18936d85-8f6b-7597-87ef-62c4c5211298/source/100x100bb.jpg",
releaseDate: "2003-06-24T07:00:00Z",
primaryGenreName: "Rock",
url:
"https://www.youtube.com/embed/ViwtNLUqkMY?rel=0&controls=0&showinfo=0"
}
];
app.get("/", (req, res) => {
res.send("Hello world!");
});
// GET /albums - should return all the albums
app.get("/albums", (req, res) => {
const genre = req.query.genre;
const filteredAlbums = albumsData.filter(album => album.primaryGenreName === genre);
res.send(filteredAlbums);
});
// GET /albums/:albumId - should return a single album
app.get("/albums/:albumId", (req, res) => {
const albumId = req.params.albumId;
const album = albumsData.find(album => album.albumId === albumId);
if (!album) {
res.sendStatus(404);
} else {
res.send(album);
}
});
// POST /albums - should save a new album
app.post("/albums", (req, res) => {
//console.log(req.body);
albumsData.push(req.body);
res.sendStatus(201);
});
// PUT /albums/:albumId - should update the album
app.put("/albums/:albumId", (req, res) => {
const albumId = req.params.albumId;
const album = albumsData.find(album => album.albumId === albumId);
album.artistName = "New artist";
res.sendStatus(200);
})
// DELETE /albums/:albumId - should delete the album
app.delete("/albums/:albumId", (req, res) => {
const index = albumsData.findIndex(album => album.albumId === req.params.albumId);
albumsData.splice(index, 1);
res.sendStatus(200);
})
app.listen(3000, function() {
console.log("Server is listening on port 3000. Ready to accept requests!");
});