-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
124 lines (99 loc) · 3.32 KB
/
app.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require('express');
const hbs = require('hbs');
const SpotifyWebApi = require('spotify-web-api-node');
const bodyParser = require('body-parser')
const dotenv = require('dotenv').config();
const app = express();
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
const clientId = process.env.CLIENT_ID,
scopes = ['user-read-private', 'user-read-email'],
clientSecret = process.env.CLIENT_SECRET,
redirectUri = 'localhost:5000/users/auth/spotify/redirect';
// Create the api object with the credentials
var spotifyApi = new SpotifyWebApi({
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri
});
var authorizeURL = spotifyApi.createAuthorizeURL(scopes);
app.get('/auth/spotify', (req, res) => {
res.redirect(authorizeURL)
console.log(authorizeURL);
});
app.get('/users/auth/spotify/redirect', (req, res) => {
debugger
spotifyApi.authorizationCodeGrant(req.query.code).then(
function (data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
console.log('The refresh token is ' + data.body['refresh_token']);
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.setRefreshToken(data.body['refresh_token']);
spotifyApi.getMe()
.then(function (data) {
res.render('callback', { data })
console.log(data);
})
},
function (err) {
console.log('Something went wrong!', err);
}
);
})
// Retrieve an access token.
spotifyApi.clientCredentialsGrant().then(
function (data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
},
function (err) {
console.log('Something went wrong when retrieving an access token', err);
}
);
//get
app.get("/artists", (req, res) => {
spotifyApi.searchArtists(req.query.artist)
.then(data => {
res.render("artists", { artists: data.body.artists.items })
console.log(data.body.artists.items);
})
.catch(err => {
console.log("an error occured!" + err)
})
})
//get albums
app.get('/albums/:artistId', (req, res, next) => {
const { artistId } = req.params;
spotifyApi.getArtistAlbums(artistId)
.then(data => {
res.locals.albumArray = data.body.items;
console.log(data.body.items);
res.render('album');
})
.catch(err => next(err));
});
//get album track
app.get('/tracks/:trackId', (req, res, next) => {
const { trackId } = req.params;
spotifyApi.getAlbumTracks(trackId)
.then(data => {
// res.send(data);
res.locals.tracksArray = data.body.items;
console.log('tracks id', data.body.items);
res.render("tracks");
})
.catch(err => next(err));
});
// the routes go here:
const index = require('./routes/index');
app.use('/', index);
app.set('port', (process.env.PORT || 5000));
// Start node server
app.listen(app.get('port'), function () {
console.log('"My Spotify project running on port 5000 🎧 🥁 🎸 🔊") ' + app.get('port'));
});