Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7485ac5

Browse files
SidHarderdaviddias
authored andcommitted
feat(examples): add a getting-started example
1 parent d7f9456 commit 7485ac5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

examples/gettingstarted/hello.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello, how are you today? This is just some random content.
2+
3+
e9a7fd36-d785-4c90-95ae-011329425f9a

examples/gettingstarted/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var fs = require('fs')
2+
var IPFS = require('ipfs')
3+
var node = new IPFS()
4+
5+
var fileName = './hello.txt'
6+
7+
// Display version of js-ipfs
8+
node.version(function (err, versionData) {
9+
if (!err) {
10+
console.log(versionData)
11+
// We can load the repo like this.
12+
node.load(function (err) {
13+
// If there is an erro loading the repo we can find out like this.
14+
if (err) {
15+
console.log(err)
16+
} else {
17+
console.log('The repo is loaded now.')
18+
}
19+
// Ok let's go online and do some cool stuff
20+
node.goOnline(function () {
21+
// We can test to see if we actually are online if we want to
22+
if (node.isOnline()) console.log('Yep, we are online')
23+
// Now that we are online now. Let's add a file.
24+
var readStream = fs.createReadStream(fileName)
25+
node.files.add(readStream, function (err, data) {
26+
if (!err) {
27+
// Awesome we've added a file so let's retrieve and display its contents from IPFS
28+
node.files.cat(data[0].hash, function (err, stream) {
29+
if (!err) {
30+
stream.pipe(process.stdout, { end: false })
31+
// let's call it a day now and go offline
32+
node.goOffline()
33+
} else { console.log('Oops for some reason there was a problem retrieving your file: ' + err) }
34+
})
35+
} else { console.log('Oops there was a problem: ' + err) }
36+
})
37+
})
38+
})
39+
} else { console.log(err) }
40+
})

0 commit comments

Comments
 (0)