-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (64 loc) · 2.03 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
'use strict'
const express = require('express')
const request = require('request')
const compression = require('compression')
const path = require('path')
// https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow
const options = {
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${process.env.BASE64_ENDODED_CLIENTID_COLON_CLIENTSECRET}`
},
qs: { grant_type: 'client_credentials' }
}
let parsedResult
async function getAccessToken(options) {
// (I.) promise to return the parsedResult for processing
function spotifyRequest() {
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
try {
resolve(JSON.parse(body).access_token)
} catch (e) {
reject(e)
}
})
})
}
// (II.)
try {
parsedResult = await spotifyRequest()
} catch (e) {
console.error(e)
}
return parsedResult
}
function endpointCreation() {
try {
const app = express()
const port = process.env.PORT || 5000
app.use(compression())
app.use(express.static(path.join(__dirname, 'client/build')))
// required to serve SPA on heroku production without routing problems; it will skip only 'api' calls
if (process.env.NODE_ENV === 'production') {
app.get(/^((?!(api)).)*$/, function (req, res) {
res.set('Cache-Control', 'public, max-age=31536001')
res.sendFile(path.join(__dirname, 'client/build', 'index.html'))
})
}
// Liam Neeson reference: Taken (token)
app.get('/api/liamNeeson', async (req, res) => {
res.set('Cache-Control', 'no-cache')
const accessToken = await getAccessToken(options)
res.json(accessToken)
console.log(`/api/liamNeeson endpoint has been called! ${accessToken}`)
})
app.listen(port)
console.log(`API is listening on ${port}`)
} catch (e) {
console.error(e)
}
}
endpointCreation()