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

Commit

Permalink
feat(examples): add a getting-started example
Browse files Browse the repository at this point in the history
  • Loading branch information
SidHarder authored and daviddias committed Nov 12, 2016
1 parent d7f9456 commit 7485ac5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/gettingstarted/hello.txt
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
40 changes: 40 additions & 0 deletions examples/gettingstarted/index.js
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) }
})

0 comments on commit 7485ac5

Please sign in to comment.