-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (42 loc) · 1.31 KB
/
server.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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
const content = "./content"
const config = "./config"
app.use(express.json());
app.use(express.static('public'));
app.route('/api')
.post(function (req, res) {
fs.readFile(path.join(config, "last-id"), "utf-8", (error, data) => {
if (error) { data = "0"; return };
fs.writeFile(path.join(content, data), JSON.stringify(req.body), () => {
fs.mkdir(config, () => {
fs.writeFile(path.join(config, "last-id"), +data + 1, () => {
res.status(200).send("post|ok");
});
});
});
});
})
.get(function (_, res) {
let result = [];
let data = 0;
let len_data = 0;
fs.readdir(content, (error, fileList) => {
if (error) { return; }
console.log(fileList);
for (element of fileList) {
data = fs.readFileSync(path.join(content, element), "utf-8");
result[len_data] = JSON.parse(data);
++len_data;
console.log(len_data);
//if (data_len == fileList.length)
// res.status(200).send(result);
}
console.log(result);
res.status(200).send(result);
});
});
app.listen(port);