-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patht11.js
More file actions
82 lines (56 loc) · 1.49 KB
/
t11.js
File metadata and controls
82 lines (56 loc) · 1.49 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
const http = require("http")
const qs = require("querystring")
require("./potjs/lib/pot-node")
const form = `<!DOCTYPE html><head><meta charset="UTF-8"></head>
<pre>
<form method=post action="http://localhost:3000">
key <input name=key>
value <input name=value>
<input type=submit name=button value=put>
<hr />
key <input name=search>
<input type=submit name=button value=get>
value <input name=result>
</form>
</pre>`
var kvs
; (async () => {
await pot.ready()
kvs = new pot.Kvs()
})()
async function put(key, value) {
await kvs.put(key, value)
return `put ${key}: ${value}`
}
async function get(search) {
value = await kvs.get(search)
return `get ${search}: ${value}
<script>
document.getElementsByName('result')[0].value = '${value}'
</script>`
}
const server = http.createServer(function(request, response) {
if (request.method == 'POST') {
var body = ''
request.on('data', function(data) {
body += data
})
request.on('end', async function() {
var log
const post = qs.parse(body)
if(post.button == 'put')
log = await put(post.key, post.value)
else
log = await get(post.search)
console.log("srv: " + log.match(/.*/)[0])
response.writeHead(200, {'Content-Type': 'text/html'})
response.end(form + '<hr><br><pre>\t\t' + log)
})
}
else {
response.writeHead(200, {'Content-Type': 'text/html'})
response.end(form)
}
})
.listen(3000, '127.0.0.1')
console.log(`srv: listening at http://127.0.0.1:3000`)