-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
38 lines (33 loc) · 838 Bytes
/
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
import * as http from "http";
import { compile } from "./lib";
http
.createServer(async (request, response) => {
let result;
if (request.method !== "POST") {
result = {
error: {
message: "Only POST requests are accepted",
},
};
} else {
const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
}
const data = Buffer.concat(buffers).toString();
try {
const input = JSON.parse(data);
result = compile(input);
} catch (e) {
result = {
error: {
message: e.message,
},
};
}
}
const encoded = JSON.stringify(result);
response.writeHead(200, { "Content-Type": "application/json" });
response.end(encoded, "utf-8");
})
.listen(8888);