-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (91 loc) · 2.27 KB
/
index.js
File metadata and controls
99 lines (91 loc) · 2.27 KB
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
'use strict';
const http = require('http');
const inspector = require('inspector');
const html =
`
<body>
<textarea rows="2" cols="40" id="input">
Convert me to upper case!
</textarea>
<br/>
<button type="button" onclick="CaseRequest('upper')">to upper case</button>
<button type="button" onclick="CaseRequest('lower')">to lower case</button>
</body>
<script>
async function CaseRequest(case_mode) {
let text = document.getElementById("input").value;
let request = {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "omit",
headers: { "Content-Type": "application/json" },
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify({ text, case_mode }),
};
let response = await fetch('http://localhost:8080', request);
let result = await response.json();
document.getElementById("input").value = result;
}
</script>
<style>
#input {
resize: none;
}
body {
margin:0;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
`
function ProcessInput(request) {
let {text, case_mode} = JSON.parse(request);
switch (case_mode) {
case "upper":
text = text.toUpperCase();
break;
case "lower":
text = text.toLowerCase();
break;
}
return JSON.stringify(text);
}
async function GetPostBody(request) {
return new Promise(function(resolve) {
let body = "";
request.on('data', data => body += data);
request.on('end', end => resolve(body));
});
}
async function Server(request, response) {
try {
if (request.method == 'POST') {
if (request.headers["devtools"]) {
console.log("DevTools is performing: " + request.headers["devtools"]);
}
let body = await GetPostBody(request);
let result = ProcessInput(body);
response.writeHead(200, {
'Content-Type': 'application/json',
'Server-Timing': 'server-timing'
});
response.write(result);
response.end();
} else {
response.writeHead(200, {
'Content-Type': 'text/html',
});
response.write(html);
response.end();
}
} catch (e) {
console.log(e);
}
}
// fuser -k 8080/tcp
http.createServer(Server).listen(8080);