forked from gSchool/fs-pet-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpServer.js
47 lines (46 loc) · 1.43 KB
/
httpServer.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
const http = require("http");
const fs = require("fs");
const port = 3000;
// console.log("hi");
const server = http.createServer((req, res) => {
// console.log(req.url);
// res.write("testing");
// res.end();
fs.readFile("pets.json", "UTF-8", (err, data) => {
const pets = JSON.parse(data);
const index = parseInt(req.url.slice(6));
if (err) {
console.error(err);
} else if (req.method === "GET" && req.url === "/pets")
fs.readFile("pets.json", "UTF-8", (err, data) => {
{
var petsJSON = JSON.stringify(pets);
res.writeHead(200), { "content-type": "applications/JSON" };
res.end(petsJSON);
}
});
else if (req.method === "GET" && req.url === "/pets/0")
fs.readFile("pets.json", "UTF-8", (err, data) => {
{
var petsJSONZero = JSON.stringify(pets[0]);
res.writeHead(200), { "content-type": "applications/JSON" };
res.end(petsJSONZero);
}
});
else if (req.method === "GET" && req.url === "/pets/1") {
var petsJSONOne = JSON.stringify(pets[1]);
res.writeHead(200), { "content-type": "applications/JSON" };
res.end(petsJSONOne);
} else {
res.writeHead(404, { "content-type": "text/plain" });
res.end("Not Found");
}
});
});
server.listen(port, (error) => {
if (error) {
console.error("error");
} else {
console.log(`Server is running ${port}`);
}
});