forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add a getting-started example
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hello, how are you today? This is just some random content. | ||
|
||
e9a7fd36-d785-4c90-95ae-011329425f9a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var fs = require('fs') | ||
var IPFS = require('ipfs') | ||
var node = new IPFS() | ||
|
||
var fileName = './hello.txt' | ||
|
||
// Display version of js-ipfs | ||
node.version(function (err, versionData) { | ||
if (!err) { | ||
console.log(versionData) | ||
// We can load the repo like this. | ||
node.load(function (err) { | ||
// If there is an erro loading the repo we can find out like this. | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
console.log('The repo is loaded now.') | ||
} | ||
// Ok let's go online and do some cool stuff | ||
node.goOnline(function () { | ||
// We can test to see if we actually are online if we want to | ||
if (node.isOnline()) console.log('Yep, we are online') | ||
// Now that we are online now. Let's add a file. | ||
var readStream = fs.createReadStream(fileName) | ||
node.files.add(readStream, function (err, data) { | ||
if (!err) { | ||
// Awesome we've added a file so let's retrieve and display its contents from IPFS | ||
node.files.cat(data[0].hash, function (err, stream) { | ||
if (!err) { | ||
stream.pipe(process.stdout, { end: false }) | ||
// let's call it a day now and go offline | ||
node.goOffline() | ||
} else { console.log('Oops for some reason there was a problem retrieving your file: ' + err) } | ||
}) | ||
} else { console.log('Oops there was a problem: ' + err) } | ||
}) | ||
}) | ||
}) | ||
} else { console.log(err) } | ||
}) |