-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (65 loc) · 1.87 KB
/
server.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
const express = require('express');
const SpotifyWebApi = require('spotify-web-api-node');
const cors = require('cors');
const lyricsFinder = require('lyrics-finder');
const app = express();
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/login', (req, res) => {
const { code } = req.body;
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET
});
spotifyApi
.authorizationCodeGrant(code)
.then(data => {
const { access_token, refresh_token, expires_in } = data.body;
res.json({
accessToken: access_token,
refreshToken: refresh_token,
expiresIn: expires_in
});
})
.catch(err => {
console.log('Post request failed \n', err);
res.sendStatus(400);
});
});
app.post('/refresh', (req, res) => {
const { refreshToken } = req.body;
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
refreshToken
});
spotifyApi
.refreshAccessToken()
.then(data => {
const { access_token, expires_in } = data.body;
res.json({
accessToken: access_token,
expiresIn: expires_in
});
})
.catch(err => {
console.log('Refresh token post request failed \n', err);
res.sendStatus(400);
});
});
app.get('/lyrics', async (req, res) => {
try {
const { artist, title } = req.query;
const lyrics = (await lyricsFinder(artist, title)) || 'Lyrics Unavailable';
res.json({ lyrics });
} catch (err) {
console.log('Lyrics fetching failed \n', err);
res.sendStatus(400);
}
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port: ${port}`));