forked from alt-art/lyweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (51 loc) · 1.51 KB
/
main.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
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const authToken = process.env.GENIUS_TOKEN;
const path = require('path');
const port = process.env.PORT || 3000;
const app = express();
const URL = 'https://api.genius.com';
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/search', (req, res) => {
const query = req.query.q,
page = req.query.page;
fetch(`${URL}/search?q=${query}&page=${page}`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
})
.then((response) => response.json())
.then((data) => {
const songs = data.response.hits.map((hit) => {
const {
song_art_image_thumbnail_url: songArt,
title_with_featured: title,
primary_artist: {name: artistName},
id: id,
} = hit.result;
return { songArt, title, artistName, id };
});
res.header('Content-Type', 'application/json');
res.send(JSON.stringify(songs));
});
});
app.get('/api/lyrics/:id', (req, res) => {
const id = req.params.id;
fetch(`${URL}/songs/${id}?text_format=plain`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
})
.then((response) => response.json())
.then((data) => {
const {
song: { lyrics: lyrics },
} = data.response;
res.header('Content-Type', 'application/json');
res.send(JSON.stringify(lyrics));
});
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});