-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (72 loc) · 2 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const http = require("http");
const url = require("url");
const parse = require("querystring");
const promisify = require("util").promisify;
const fs = require("fs");
const open = promisify(fs.open);
const close = promisify(fs.close);
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const unlink = promisify(fs.unlink);
const theData = require("./lib/data");
const port = 3000;
const host = "127.0.0.1";
//creating server
const httpServer = http.createServer((req, res) => {
if (req.method === "POST") {
Post(req, res);
}
if (req.method === "GET") {
Get(req, res);
}
});
httpServer.listen(port, host, () => {
console.log(`Server running on port: ${port} host: ${host}`);
});
function Post(req, res) {
var body = "";
req.on("data", function(datachunk) {
body += datachunk.toString();
});
req.on("end", () => {
readFile(`./data/${req.url}/data.json`, "utf-8")
.then(data => {
stringData = JSON.parse(body);
data = JSON.parse(data);
data.users.push(stringData);
return writeFile(
`./data/${req.url}/data.json`,
JSON.stringify(data, null, "\t")
);
})
.then(() => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end(`Posted!`);
});
});
}
function Get(req, res) {
if (req.url != "/users") {
pathArr = req.url.split("/");
path = pathArr[1];
id = pathArr[2];
readFile(`./data/${path}/data.json`, "utf-8").then(data => {
data = JSON.parse(data);
data.users.forEach(item => {
if (item.id == id) {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(item));
}
});
});
} else {
readFile(`./data/${req.url}/data.json`, "utf-8").then(data => {
data = JSON.parse(data);
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(data));
});
}
}