/
index.js
50 lines (41 loc) · 965 Bytes
/
index.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
import Express from "express";
import fs from "fs";
import dinobase from "./dinosaurs.js";
let about_txt;
fs.readFile("./static_content/about.txt", "utf8", (err, data) => {
if (err) {
return;
}
about_txt = data;
});
let endpoints;
fs.readFile("./static_content/endpoints.json", (err, data) => {
if (err) {
return;
}
endpoints = data;
});
const app = Express();
const port = 1212;
app.get("/", (req, res) => {
res.send("Hello World");
});
app.get("/about", (req, res) => {
res.end(about_txt);
});
app.get("/endpoints", (req, res) => {
res.setHeader("Content-Type", "application/json");
res.end(endpoints);
});
app.get("/dinosaur/:dinoName", (req, res) => {
dinobase.forEach((dino) => {
if (dino.name == req.params.dinoName) {
res.json(dino);
}
});
res.send("not found");
});
app.get("/random", (req, res) => {
res.send(dinobase[Math.floor(Math.random() * dinobase.length)]);
});
app.listen(port, () => {});