-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample9.js
More file actions
103 lines (65 loc) · 1.98 KB
/
Copy pathexample9.js
File metadata and controls
103 lines (65 loc) · 1.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
console.log(`
POT JS Example 9: Node.js, local Swarm network, synchronous
This is a self-contained web app, serving a page to receive input,
returning the results.
Functionally identical to example 8, just: started with different
arguments — a network url and a batch id — by the calling make rule;
and POT calls used are synchronously blocking instead of promises.
open http://localhost:3000
`)
const http = require("http")
const qs = require("querystring")
require("./lib/pot-node")(3)
const form = `<!DOCTYPE html><head><meta charset="UTF-8"></head>
<pre>
POT JS Example 9: Node.js Web App / local Swarm network
<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>
<hr />
</pre>
<script> console.log('see server log in terminal') </script>
<pre>
`
var kvs
global.onPotInitialized = () => {
bee = process.argv[2]
batch = process.argv[3]
kvs = new pot.Kvs(bee, batch)
// There is no catching of early calls of put() and get()
// in this example, which in theory could race the loading
// of pot.wasm and the creation of kvs.
}
function put(key, value) {
kvs.putSync(key, value)
return `put ${key}: ${value}`
}
function get(search) {
value = kvs.getSync(search)
return `get ${search}: ${value}
<script>
document.getElementsByName('result')[0].value = '${value}'
</script>`
}
const server = http.createServer((request, response) => {
var log = ''
var body = ''
request.on('data', (data) => body += data)
.on('end', () => {
const { button, key, value, search } = qs.parse(body)
switch(button) {
case 'put': log = put(key, value) ; break
case 'get': log = get(search)
}
response.writeHead(200)
response.end(form + log)
})
})
.listen(3000, '127.0.0.1')
console.log('srv: listening at http://127.0.0.1:3000')