From 82849b00f2fda6e2b726adf4dca24c8e6f3c7a0c Mon Sep 17 00:00:00 2001 From: tsekamaru <67861937+tsekamaru@users.noreply.github.com> Date: Tue, 25 Feb 2025 21:40:58 +0100 Subject: [PATCH 1/2] done --- app.js | 48 +++++++++++++++++++++++++++------ package.json | 6 +++++ views/albums.hbs | 33 +++++++++++++++++++++++ views/artist-search-results.hbs | 18 +++++++++++++ views/home.hbs | 22 +++++++++++++++ views/layout.hbs | 36 +++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 views/albums.hbs create mode 100644 views/artist-search-results.hbs create mode 100644 views/home.hbs create mode 100644 views/layout.hbs diff --git a/app.js b/app.js index 5ea8eb4db..0a559c311 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,50 @@ -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.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..57af44d7f --- /dev/null +++ b/views/albums.hbs @@ -0,0 +1,33 @@ +
By {{album.artists.[0].name}}
+ +