-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.server.js
102 lines (75 loc) · 2.58 KB
/
debug.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
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
const express = require('express');
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
const log = (data) => {
var time = data.time;
var type = data.type;
var subject = data.subject;
var message = data.message;
return `[${time.toString()}|${(type === 'error' ? 'error' : subject).toString().toUpperCase()}] ${message}`;
}
class DebugServer {
app = {};
port = 8088;
constructor(port) {
this.app = express();
this.port = parseInt(port ? port : this.port);
console.log(`Server started on port ${this.getUrl()}`);
this.app.use(
function (req, res)
{
req.get('/favicon.ico', function (req2, res2)
{
res2.sendStatus(204);
res2.end();
}
);
const hasQuery = Object.keys(req.query).length > 0;
const hasHeaders = Object.keys(req.headers).length > 0;
var data = hasQuery ? req.query : {};
data.headers = hasHeaders ? req.headers : {}; // headers
var output = log(data); // create output for console
console.log(
output && typeof output === 'object' ? // if output is an object
`${JSON.stringify(data, getCircularReplacer())}` : // true => json data
`${output}` // false => message
);
res.setHeader('Accept', 'application/json');
res.setHeader('Cache-Control', 's-max-age=1, no-cache');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Header', '*');
res.send(JSON.stringify(data, getCircularReplacer()));
}
);
this.app.listen(this.port);
return this;
}
stop = () => {
this.app.close();
}
getApp = () => {
return this.app;
}
getPort = () => {
return this.port;
}
getUrl = () => {
return `http://localhost:${this.port}`;
}
getUrlSecure = () => {
return `https://localhost:${this.port}`;
}
}
// Run it in the terminal with "$ npm run debug"
// a nodejs command line debugger will spawn
new DebugServer(8080);