-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmain.js
More file actions
141 lines (122 loc) · 3.98 KB
/
main.js
File metadata and controls
141 lines (122 loc) · 3.98 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
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
function html(strings, ...values) {
return strings
.map((string, index) => {
let value = values[index]
if (value === undefined) return string
if (typeof value === "function") value = value()
return string + value
})
.join("")
}
const fs = require("fs")
const { exec } = require("child_process")
const serverModTime = fs.statSync(__filename).mtime
const serverStartTime = new Date()
function indexPage() {
return html`
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Hello from Node!</title>
<h1>Hello from Node!</h1>
<p>
Edit this app
<button onclick="editCode()">Edit Code</button>
<script>
const editCode = async () => await fetch("/edit", { method: "POST" })
</script>
</p>
<form id="apiForm">
<label for="inputText">Type something:</label>
<input type="text" id="inputText" />
<button type="submit">Send to server</button>
</form>
<div id="outputBox">
<h3>Server response:</h3>
<pre id="outputText"></pre>
</div>
<li>
Address:
<script>
document.write(location)
</script>
</li>
<li>Modified at: ${serverModTime}</li>
<li>Started at: ${serverStartTime}</li>
<li>
Loaded at: ${new Date()}
<h3>use fetch to load JSON when I click this button: <button onclick="loadJson()">Load JSON</button></h3>
<pre id="json"></pre>
<script>
const loadJson = () =>
fetch("/index.json")
.then(r => r.json())
.then(j => (document.querySelector("#json").innerText = JSON.stringify(j, null, 2)))
</script>
<form action="/kill" method="post">Don't <button type="submit">Kill the server</button></form>
</li>
<script>
document.querySelector("#apiForm").addEventListener("submit", async e => {
e.preventDefault()
const inputText = document.querySelector("#inputText").value
const response = await fetch("/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ text: inputText }),
})
const transformedText = await response.json()
document.querySelector("#outputText").innerText = transformedText.text
})
</script>
`
}
const router = (request, response) => {
if (request.url === "/") request.url = "/index.html"
if (request.url === "/index.html") {
response.writeHead(200, { "Content-Type": "text/html" })
response.write(indexPage(request))
response.end()
return
}
if (request.url === "/index.json") {
response.writeHead(200, { "Content-Type": "application/json" })
response.write(JSON.stringify({ hello: "world", time: new Date() }))
response.end()
return
}
if (request.url === "/kill" && request.method === "POST") {
response.writeHead(200, { "Content-Type": "text/html" })
response.write("Killing server...")
response.end()
setTimeout(() => webviewServer.close(), 0)
return
}
if (request.url === "/api" && request.method === "POST") {
let body = ""
request.on("data", chunk => {
body += chunk
})
request.on("end", async () => {
const { text } = JSON.parse(body)
const transformedText = text.split("").reverse().join("")
response.writeHead(200, { "Content-Type": "application/json" })
response.write(JSON.stringify({ text: transformedText }))
response.end()
})
return
}
if (request.url === "/edit" && request.method === "POST") {
exec(`/usr/local/bin/code "${__dirname}/../.."`, err => {
response.writeHead(err ? 500 : 200, { "Content-Type": "application/json" })
response.write(
JSON.stringify(err ? { error: "Failed to execute the command." } : { success: "Command executed successfully." }),
)
response.end()
})
return
}
response.writeHead(404)
response.end(`Not found: ${request.url}`)
}
const webviewServer = require("./lib/http-webview").create(router)