forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
52 lines (42 loc) · 2.04 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>IPFS in the Browser</title>
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
<script type="text/javascript">
const node = new Ipfs({ repo: 'ipfs-' + Math.random() })
node.once('ready', () => {
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
// You can write more code here to use it. Use methods like
// node.add, node.get. See the API docs here:
// https://github.com/ipfs/interface-ipfs-core
})
</script>
</head>
<body>
<h1>IPFS in the Browser</h1>
<p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <b><em style="background-color:#d7d6d6">node</em></b>. Open the console to play around with it.</p>
<p>Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.</p>
<h1 id="status">Node status: offline</h1>
<h2>Some suggestions</h2>
<p>Try adding a new file:</p>
<code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
node.add(new node.types.Buffer('Hello world!'), (err, filesAdded) => {
if (err) {
return console.error('Error - ipfs add', err, res)
}
filesAdded.forEach((file) => console.log('successfully stored', file.hash))
})
</code>
<p>You can cat that same file. If you used the exact same string as above ('Hello world!') you should have an hash like this: 'QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY'</p>
<code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
node.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY', function (err, data) {
if (err) {
return console.error('Error - ipfs files cat', err, res)
}
console.log(data.toString())
})
</code>
</body>
</html>