-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (31 loc) · 1.23 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
const express = require('express')
const request = require('request')
require('dotenv').config()
const app = express()
app.set('port', (process.env.PORT || 3001));
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
function formatTracks(body) {
let tracks = [];
let jsonBody = JSON.parse(body)
jsonBody.tracks.track.forEach((track, index) => {
tracks.push({name: track.name, listeners: track.listeners, artist: track.artist.name, rank: parseInt(track["@attr"].rank)+1, image: track.image[1]["#text"]});
})
return {tracks};
}
app.get('/api/country/:country', function(req, res) {
request(`http://ws.audioscrobbler.com/2.0/?method=geo.gettoptracks&country=${req.params.country}&api_key=${process.env.LAST_FM_API_KEY}&format=json&limit=5`, (error, response, body) => {
if (error) {
const status = response && response.statusCode ? response.statusCode : 500;
res.status(status).json({error: error});
} else if (JSON.parse(body).error) {
res.status(500).json({error: JSON.parse(body).message});
} else {
res.status(200).json(formatTracks(body));
}
})
})
app.listen(app.get('port'), function () {
console.log(`Example app listening on port ${app.get('port')}!`)
})