-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (44 loc) · 1.29 KB
/
index.js
File metadata and controls
50 lines (44 loc) · 1.29 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
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
const app = express();
const port = 3000;
const API_URL = "https://api.lyrics.ovh/v1";
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.post("/", async (req, res) => {
const searchArtist = req.body.artist.trim();
const searchTitle = req.body.title.trim();
try {
const result = await axios.get(
`${API_URL}/${encodeURIComponent(searchArtist)}/${encodeURIComponent(
searchTitle
)}`
);
const newLyrics = result.data.lyrics.replace(/\n/g, "<br>");
res.render("index.ejs", {
artist: searchArtist,
title: searchTitle,
lyrics: newLyrics,
});
} catch (error) {
if (error.response) {
const retryAfter = error.response.headers["retry-after"];
if (retryAfter) {
console.log(`Rate limited. Retry after: ${retryAfter} seconds`);
} else {
console.log(
"No retry-after header found. Check other headers or wait."
);
}
} else {
console.error("API request failed for another reason:", error.message);
}
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});