Skip to content

Commit bbd6d6a

Browse files
committed
Week 2 Lessons 3.2: Get Album by ID
We achieved that using req.params.albumId and storing the value in a variable called albumIdToFind. Then using the .find method on the albumsData to find the album that has the matching id or albumIdToFind and then display that single album. Tested by sending: localhost:35981/albums/11 to get the album that has 11 as its id.
1 parent fdc7b3b commit bbd6d6a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require("express");
22
const app = express();
3+
app.use(express.json());
34

45
const albumsData = [
56
{
@@ -64,6 +65,14 @@ app.get("/albums", function (req, res) {
6465
res.send({ albumsData });
6566
});
6667

68+
app.get("/albums/:albumId", (req,res) => {
69+
const albumIdToFind = req.params.albumId;
70+
console.log(req.params.albumId);
71+
const albumMatchingID = albumsData.find((album) => album.albumId === albumIdToFind);
72+
console.log(albumMatchingID);
73+
res.send({ albumMatchingID })
74+
})
75+
6776
app.listen(35981, function () {
6877
console.log("Server is listening on port 35981. Ready to accept requests!");
6978
});

0 commit comments

Comments
 (0)