diff --git a/app.js b/app.js index 5ea8eb4db..f846e86a0 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,73 @@ -require('dotenv').config(); +require("dotenv").config(); -const express = require('express'); -const hbs = require('hbs'); +const express = require("express"); +const hbs = require("hbs"); // require spotify-web-api-node package here: - +const SpotifyWebApi = require("spotify-web-api-node"); const app = express(); -app.set('view engine', 'hbs'); -app.set('views', __dirname + '/views'); -app.use(express.static(__dirname + '/public')); +app.set("view engine", "hbs"); +app.set("views", __dirname + "/views"); +app.use(express.static(__dirname + "/public")); // setting the spotify-api goes here: +const spotifyApi = new SpotifyWebApi({ + clientId: process.env.CLIENT_ID, + clientSecret: process.env.CLIENT_SECRET, +}); + +// Retrieve an access token +spotifyApi + .clientCredentialsGrant() + .then((data) => spotifyApi.setAccessToken(data.body["access_token"])) + .catch((error) => console.log("Something went wrong when retrieving an access token", error)); // Our routes go here: +app.get("/", (req, res) => { + res.render("home"); +}); + +app.get("/artist-search", (req, res) => { + spotifyApi + .searchArtists(req.query.artist) + .then((data) => { + const artists = data.body.artists.items; + res.status(200).render("artist-search-results", { artists }); + }) + .catch((err) => console.log("The error while searching artists occurred: ", err)); +}); + +app.get("/albums/:artistId", (req, res, next) => { + spotifyApi.getArtistAlbums(req.params.artistId).then((data) => { + console.log("Artist albums", data.body.items); + const albums = data.body.items; + res.render("albums", { albums }); + }); +}); + +app.get("/tracks/:albumId", (req, res, next) => { + spotifyApi + .getAlbumTracks(req.params.albumId) + .then((data) => { + const tracks = data.body.items; + // Get album info to display album name and image + spotifyApi + .getAlbum(req.params.albumId) + .then((albumData) => { + const albumInfo = albumData.body; + res.render("tracks", { tracks, album: albumInfo }); + }) + .catch((error) => { + console.error("Error getting album info:", error); + next(error); + }); + }) + .catch((error) => { + console.error("Error getting album tracks:", error); + next(error); + }); +}); -app.listen(3000, () => console.log('My Spotify project running on port 3000 🎧 🥁 🎸 🔊')); +// Start listening the server +app.listen(3000, () => console.log("My Spotify project running on port 3000 🎧 🥁 🎸 🔊")); diff --git a/package.json b/package.json index c9f4085ba..c6ef96d20 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,11 @@ "license": "ISC", "devDependencies": { "nodemon": "^2.0.2" + }, + "dependencies": { + "dotenv": "^16.4.7", + "express": "^4.21.2", + "hbs": "^4.2.0", + "spotify-web-api-node": "^5.0.2" } } diff --git a/views/albums.hbs b/views/albums.hbs new file mode 100644 index 000000000..036201a19 --- /dev/null +++ b/views/albums.hbs @@ -0,0 +1,33 @@ +
By {{album.artists.[0].name}}
+ +