-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (68 loc) · 2.24 KB
/
Copy pathserver.js
File metadata and controls
76 lines (68 loc) · 2.24 KB
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
const path = require('path')
const express = require('express')
const request = require('request')
// const querystring = require('querystring')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
require('dotenv').config()
const clientId = process.env.CLIENT_ID
const clientSecret = process.env.CLIENT_SECRET
// const redirect_uri = 'http://localhost:8888/callback'
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'dist')))
app.use(cookieParser())
app.get('/access-spotify', function (req, res) {
request.post({
url: 'https://accounts.spotify.com/api/token',
headers: { 'Authorization': 'Basic ' + (Buffer.from(clientId + ':' + clientSecret).toString('base64')) },
form: {
grant_type: 'client_credentials'
},
json: true
}, function (error, response, body) {
if (error) res.send(`error: ${response.statusCode}`)
res.send(body.access_token)
})
})
app.post('/get-albums', function (req, res) {
const artist = req.body.artist
const token = req.body.token
request.get({
url: `https://api.spotify.com/v1/search?q=artist:${artist}&type=album`,
headers: { 'Authorization': 'Bearer ' + token },
json: true
}, function (error, response, body) {
if (error) res.send(`error: ${response.statusCode}`)
res.json(body)
})
})
app.post('/get-trackids', function (req, res) {
const albumId = req.body.albumId
const token = req.body.token
request.get({
url: `https://api.spotify.com/v1/albums/${albumId}/tracks`,
headers: { 'Authorization': 'Bearer ' + token },
json: true
}, function (error, response, body) {
if (error) res.send(`error: ${response.statusCode}`)
if(body !== null)
res.json(body)
})
})
app.post('/get-tracks', function (req, res) {
const trackIds = req.body.trackIds
const token = req.body.token
request.get({
url: `https://api.spotify.com/v1/tracks/?ids=${trackIds}`,
headers: { 'Authorization': 'Bearer ' + token },
json: true
}, function (error, response, body) {
if (error) res.send(`error: ${response.statusCode}`)
if(body !== null)
res.json(body)
})
})
console.log('Listening on 8888')
app.listen(8888)