-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 1.67 KB
/
Copy pathindex.js
File metadata and controls
68 lines (59 loc) · 1.67 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
const express = require("express");
const { findLyrics } = require("./lyrics");
const cors = require("cors");
const appApi = express();
const appFrontend = express();
const portApi = 8080;
const portFrontend = 8081;
appApi.use(cors());
// Decode + as space in path params (some clients use + instead of %20)
appApi.use("/v1", function (req, res, next) {
req.url = req.url.replace(/\+/g, "%20");
next();
});
const GARBAGE = new Set([
"artist",
"title",
"unknown",
"undefined",
"null",
"no song playing",
"_",
"",
]);
appApi.get("/v1/:artist/:title", function (req, res) {
const artist = req.params.artist;
const title = req.params.title;
if (!artist || !title) {
return res.status(400).send({ error: "Artist or title missing" });
}
if (GARBAGE.has(artist.toLowerCase()) || GARBAGE.has(title.toLowerCase())) {
return res.status(400).send({ error: "Invalid artist or title" });
}
findLyrics(title, artist)
.then((l) => {
res.send({ lyrics: l });
})
.catch((e) => {
res.status(404).send({ error: "No lyrics found" });
});
});
appApi.get("/suggest/:term", async function (req, res) {
try {
const response = await fetch(
"http://api.deezer.com/search?limit=15&q=" +
encodeURIComponent(req.params.term),
);
const results = await response.json();
res.send(results);
} catch (e) {
res.status(500).send({ error: "Failed to fetch suggestions" });
}
});
appFrontend.use(express.static("frontend"));
appApi.listen(portApi, function () {
console.log("API listening on port " + portApi);
});
appFrontend.listen(portFrontend, function () {
console.log("Frontend listening on port " + portFrontend);
});