-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (60 loc) · 1.67 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
require("dotenv").config();
const express = require("express");
const SpotifyWebApi = require("spotify-web-api-node");
const cors = require("cors");
const bodyParser = require("body-parser");
const lyricsFinder = require("lyrics-finder");
const port = process.env.PORT || 8080;
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/refresh", (req, res) => {
const refreshToken = req.body.refreshToken;
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken,
});
spotifyApi
.refreshAccessToken()
.then((data) => {
console.log("token was refreshed", data.body);
res.json({
accessToken: data.body.accessToken,
expiresIn: data.body.expiresIn,
});
})
.catch(() => {
res.sendStatus(400);
});
});
app.post("/login", (req, res) => {
const code = req.body.code;
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
spotifyApi
.authorizationCodeGrant(code)
.then((data) => {
res.json({
accessToken: data.body.access_token,
refreshToken: data.body.refresh_token,
expiresIn: data.body.expires_in,
});
})
.catch((err) => {
console.log(err);
res.sendStatus(400);
});
});
app.get("/lyrics", async (req, res) => {
const lyrics =
(await lyricsFinder(req.query.artist, req.query.track)) ||
"No lyrics found";
res.json({ lyrics });
});
app.listen(port);