-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (29 loc) · 908 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
const server = require("http").createServer();
const fs = require("fs");
const superagent = require('superagent');
let temp = fs.readFileSync(`${__dirname}/index.html`, "utf-8");
const Replace = (tmp, data) => {
let output = tmp.replace(/{%IMAGE%}/g, data);
return output;
}
server.on("request", (req, res) => {
const path = req.url;
if (path === "/" || path === "/home" || path === "/index") {
res.writeHead(200);
res.end("Hello")
} else if (path === "/dogs") {
res.writeHead(200, {
"Content-type": "text/html"
});
superagent
.get("https://dog.ceo/api/breeds/image/random")
.end((err, img) => {
let page = Replace(temp, img.body.message);
res.end(page)
})
} else {
res.writeHead(404);
res.end("page not found")
}
})
server.listen()