From ab6e45d71cf7eb69001e2a99f2fc1427bb33b554 Mon Sep 17 00:00:00 2001 From: jopen Date: Wed, 17 Sep 2025 11:55:30 +0200 Subject: [PATCH] logica del servidor usando express, handelbars --- app.js | 55 +++++++++++++++++++++++++++++++++ package.json | 8 ++++- views/album.hbs | 8 +++++ views/artist-search-results.hbs | 16 ++++++++++ views/index.hbs | 7 +++++ views/layout.hbs | 15 +++++++++ views/view-tracks.hbs | 10 ++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 views/album.hbs create mode 100644 views/artist-search-results.hbs create mode 100644 views/index.hbs create mode 100644 views/layout.hbs create mode 100644 views/view-tracks.hbs diff --git a/app.js b/app.js index 5ea8eb4db..23a05c4e0 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,8 @@ 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'); @@ -13,6 +15,59 @@ 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 +}); + +//extraer token de acceso +spotifyApi + .clientCredentialsGrant() + .then(data => spotifyApi.setAccessToken(data.body['access_token'])) + .catch(error => console.log('Something went wrong when retrieving an access token', error)); + +//middleware + + // Our routes go here: +app.get('/', (req, res) => + res.render('index', {title: 'Spotify lab'}) +) + +app.get('/artist-search', (req, res) => { + const { artist } = req.query; + + spotifyApi + .searchArtists(artist) + .then(data => { + console.log('The received data from the API: ', data.body); + res.render('artist-search-results', { artists: data.body.artists.items }) + }) + .catch(err => console.log('The error while searching artists occurred: ', err)); +}) + +app.get('/albums/:artistId', (req, res) => { + const { artistId } = req.params; + spotifyApi + .getArtistAlbums(artistId) + .then(data => { + res.render('album', { albums: data.body.items }); // busca views/album.hbs + }) + .catch(err => console.log(err)); +}); + +app.get('/view-tracks/:albumId', (req, res) => { + const { albumId } = req.params + spotifyApi + .getAlbumTracks(albumId, { limit: 20, market: 'ES'}) + .then(data => { + console.log(data.body) + res.render('view-tracks', {tracks: data.body.items})}) + .then(err => console.log('Algo fue mal', err)) +}) + + + + app.listen(3000, () => console.log('My Spotify project running on port 3000 🎧 🥁 🎸 🔊')); diff --git a/package.json b/package.json index c9f4085ba..28fe95c35 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,12 @@ "author": "", "license": "ISC", "devDependencies": { - "nodemon": "^2.0.2" + "nodemon": "^3.1.10" + }, + "dependencies": { + "dotenv": "^17.2.2", + "express": "^5.1.0", + "hbs": "^4.2.0", + "spotify-web-api-node": "^5.0.2" } } diff --git a/views/album.hbs b/views/album.hbs new file mode 100644 index 000000000..05d32c196 --- /dev/null +++ b/views/album.hbs @@ -0,0 +1,8 @@ +

Albumes

+ +{{#each albums}} +

{{this.name}}

+ {{this.name}} + Ver Tracks + +{{/each}} \ No newline at end of file diff --git a/views/artist-search-results.hbs b/views/artist-search-results.hbs new file mode 100644 index 000000000..8d06d092a --- /dev/null +++ b/views/artist-search-results.hbs @@ -0,0 +1,16 @@ +

Resultados de la busqueda sencillo

+ +{{#each artists}} +
+

{{this.name}}

+ {{this.name}} + Ver álbumes +
+{{/each}} + + + + + + + diff --git a/views/index.hbs b/views/index.hbs new file mode 100644 index 000000000..951ceb429 --- /dev/null +++ b/views/index.hbs @@ -0,0 +1,7 @@ + +

Buscar artistas

+
+ + + +
\ No newline at end of file diff --git a/views/layout.hbs b/views/layout.hbs new file mode 100644 index 000000000..628a76cc8 --- /dev/null +++ b/views/layout.hbs @@ -0,0 +1,15 @@ + + + + + + {{title}} + + {{!-- Si tienes CSS en /public --}} + + + + {{!-- Aquí se inyecta la vista (index.hbs, artist-search.hbs, etc.) --}} + {{{body}}} + + \ No newline at end of file diff --git a/views/view-tracks.hbs b/views/view-tracks.hbs new file mode 100644 index 000000000..ead7b02ec --- /dev/null +++ b/views/view-tracks.hbs @@ -0,0 +1,10 @@ +

Tracks

+ +{{#each tracks}} + +{{/each}} + + \ No newline at end of file