-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
71 lines (61 loc) · 2.08 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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const cors = require('cors');
const fetch = require('node-fetch')
const mongoose = require('mongoose');
const {asyncHandler, getSpotifyAccessToken} = require('./utils');
const port = process.env.PORT || 8080;
const {database, environment} = require('./config');
const Artist = require('./db/models/artist');
const app = express();
// const cdb = 'mongodb+srv://testuser:testpassword@rappamappadb-g8rkp.mongodb.net/RappaMappa?retryWrites=true&w=majority';
mongoose
.connect(database, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log("Connected to MongoDB successfully"))
.catch((err) => console.log(err));
//get and refresh spotify access token every hour
let spotifyAccessToken, spotifyAccessTokenVal;
(async ()=>{
spotifyAccessToken = await getSpotifyAccessToken();
spotifyAccessTokenVal = spotifyAccessToken.access_token;
})();
setInterval(async ()=>{
spotifyAccessToken = await getSpotifyAccessToken();
spotifyAccessTokenVal = spotifyAccessToken.access_token;
}, 3500000);
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
if (environment === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
}
app.get(
"/artists/",
asyncHandler(async (req, res) => {
const allArtists = await Artist.find({});
console.log("ALL ARTISTS::", allArtists);
res.send({ allArtists });
})
);
//TODO add async handler
app.get("/spotify/more-info/:artist", asyncHandler(async (req, res)=>{
const artistName = req.params.artist;
const data = await fetch(
`https://api.spotify.com/v1/search?q=${artistName}&type=artist&limit=1&offset=0`,
{
headers: {
Authorization: `Bearer ${spotifyAccessTokenVal}`,
},
}
);
const json = await data.json();
const artistInfo = json.artists.items[0];
res.send({artistInfo});
}));
app.listen(port, () => console.log(`Listening on port ${port}`));