-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (40 loc) · 1.47 KB
/
index.js
File metadata and controls
50 lines (40 loc) · 1.47 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 bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
app.set("view engine", "ejs")
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
const response = await axios.get("https://api.jikan.moe/v4/top/anime");
const result = response.data.data;
res.render("index.ejs", { data: result });
});
app.get("/search", async (req, res) => {
try {
const query = req.query.search;
const response = await axios.get("https://api.jikan.moe/v4/anime?q=" + query);
const result = response.data.data;
console.log(result);
res.render("index.ejs", { search: result, query });
} catch (error) {
console.error("Unable to retrieve anime information:", error.message);
res.render("index.ejs", {query: query });
}
});
app.get("/random", async(req, res) => {
const response = await axios.get("https://api.jikan.moe/v4/random/anime");
const result = response.data.data;
console.log(result);
res.render("random.ejs", {random: result});
});
app.get("/anime/:id", async(req, res) => {
const id = req.params.id;
const response = await axios.get("https://api.jikan.moe/v4/anime/" + id);
const result = response.data.data;
res.render("anime.ejs", { anime: result, id: id });
});
app.listen(port, () => {
console.log(`Server running on port ${port}.`);
})