-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (146 loc) · 4.92 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const express = require("express");
const fs = require('fs');
const Datastore = require("nedb");
const { version } = require("process");
const app = express();
const orders = new Datastore("data/orders.db");
orders.loadDatabase();
function FileHandler() {
this.createEmpty = function(url) {
fs.open(url, "w", function (err) {
if (err) {throw err;}
});
}
this.create = function(url, text) { // bzw replace, bzw overwrite
fs.writeFile(url, text, function (err) {
if (err) {throw err;}
});
}
this.read = function(url) {
return(fs.readFileSync(url, 'utf8', function(error) {
if(error) throw error;
}));
}
this.append = function(url, text) {
fs.appendFile(url, text, function (err) {
if (err) {throw err;}
});
}
this.replace = function(url, text) { // bzw overwrite
this.create(url, text);
}
this.remove = function(url) {
fs.unlink(url, function (err) {
if (err) throw err;
});
}
this.rename = function(url, newUrl) {
fs.rename(url, newUrl, function (err) {
if (err) throw err;
});
}
}
const fh = new FileHandler();
const port = 80;
app.listen(port, () => console.log('>Server is listening on port ' + port + "."));
app.use(express.static('./public/'));
app.use(express.json({limit: "5mb"}));
function server1() {
app.get("/getTables", function(request, response) {
let obj = JSON.parse(fh.read("data/tables.json"));
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(obj);
});
app.get("/getItems", function(request, response) {
let obj = JSON.parse(fh.read("data/items.json"));
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(obj);
});
app.get("/getConfig", function(request, response) {
let obj = JSON.parse(fh.read("data/config.json"));
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(obj);
});
app.post("/registerOrder", function(request, response) {
let config = JSON.parse(fh.read("./data/config.json"));
let oID = config.nextOrderID;
let timestamp = Date.now();
let meta = {};
meta.orderID = oID;
meta.opened = timestamp;
meta.lastEdited = timestamp;
meta.status = "open";
let data = Object.assign(meta, request.body);
orders.insert(data);
config.nextOrderID = oID+1;
config.lastEdited = timestamp;
oID = oID + 1;
fh.replace("data/config.json", JSON.stringify(config));
// response.contentType('application/json');
// response.setHeader("Access-Control-Allow-Origin", "*");
// response.json({status: 200}); // 200 = ok/successful
});
app.post("/getOpen",function(request, response) {
let options = request.body;
orders.find({status:"open"}).sort({lastEdited: 1}).limit(options.limit).exec((err, data) => {
if(err) {
response.end();
return err;
}
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(data);
});
});
app.post("/getInProgress",function(request, response) {
let options = request.body;
orders.find({status:"in-progress"}).sort({lastEdited: 1}).limit(options.limit).exec((err, data) => {
if(err) {
response.end();
return err;
}
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(data);
});
});
app.post("/getReady",function(request, response) {
let options = request.body;
orders.find({status:"ready"}).sort({lastEdited: 1}).limit(options.limit).exec((err, data) => {
if(err) {
response.end();
return err;
}
response.contentType('application/json');
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(data);
});
});
app.post("/getAll", function handleData(request, response) {
database.find({}, (err, data) => {
if(err) {
response.end();
return err;
}
response.json(data);
});
});
}
server1();
/* app.post("/api", function handleData(request, response) { //client->server
var data = request.body;
database.insert(data);
response.json(data);
});
app.get("/api", function handleData(request, response) {//server->client
database.find({}, (err, data) => {
if(err) {
response.end();
return err;
}
response.json(data);
});
}); */