|
| 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