-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.js
78 lines (68 loc) · 1.87 KB
/
Application.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
const Broker = require("./Broker.js");
function APPLICATION_LOG(message)
{
console.log(`Application ${message}`);
}
module.exports =
class Application
{
constructor()
{
this.broker = new Broker();
}
processRequest(request, response)
{
const { method, url } = request;
/*
* Headers.
for (var id in request.headers)
{
const header = request.headers[id];
APPLICATION_LOG(`received header: '${id}=${header}'`)
}
*/
// Process "POST" requests.
if (method === "POST")
{
var body = [];
// Get body.
request.on(
"data",
(chunk) => {
body.push(chunk);
}
).on(
"end",
() => {
var sbody = Buffer.concat(body).toString();
APPLICATION_LOG(`body length: '${sbody.length}' body: '${sbody}'`);
var data = "nothing"
try
{
var json = JSON.parse(sbody);
data = this.broker.process(json);
}
catch (error)
{
APPLICATION_LOG(`Error: '${error}'`)
}
this.reply(response, data);
}
);
}
// Process other request.
else
{
this.reply(response, "DebugBroker");
}
}
reply(response, data)
{
response.statusCode = 200;
response.setHeader("Content-Type", "application/json");
// Allow CORS.
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.end(data);
}
}