-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs/improve newcomers experience #723
Changes from all commits
0aa32b8
6a2d444
2b07177
694f91c
f88ae95
b7ce4a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `js-ipfs` Examples and Tutorials | ||
|
||
In this folder, you can find a variety of examples to help you get started in using js-ipfs, in Node.js and in the Browser. Every example as a specific purpose and some of each incorporate a full tutorial that you can follow through, helping you expand your knowledge about IPFS and the Distributed Web in General. | ||
|
||
Let us know if you find any issue or if you want to contribute and add a new tutorial, feel welcome to submit a PR, thank you! | ||
|
||
## Examples | ||
|
||
- [js-ipfs basic, how to spawn a node and add a file to IPFS](/examples) | ||
- [How to bundle js-ipfs with Browserify](/bundle-browserify) | ||
- [How to bundle js-ipfs with WebPack](/bundle-webpack) | ||
|
||
## Tutorials |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, how are you today? Welcome to the Distributed Web! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const os = require('os') | ||
const series = require('async/series') | ||
const IPFS = require('../../src/core') // replace this by line below | ||
// const IPFS = require('ipfs') | ||
|
||
/* | ||
* Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs) | ||
*/ | ||
const node = new IPFS(os.tmpDir() + '/' + new Date().toString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
|
||
const fileToAdd = { | ||
path: 'hello.txt', | ||
content: fs.createReadStream('./hello.txt') | ||
} | ||
|
||
let fileMultihash | ||
|
||
series([ | ||
/* | ||
* Display version of js-ipfs | ||
*/ | ||
(cb) => { | ||
node.version((err, version) => { | ||
if (err) { return cb(err) } | ||
|
||
console.log('IPFS Version:', version.version) | ||
cb() | ||
}) | ||
}, | ||
/* | ||
* Initialize the repo for this node | ||
*/ | ||
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb), | ||
/* | ||
* Load the repo config into the IPFS node | ||
*/ | ||
(cb) => node.load(cb), | ||
/* | ||
* Take the node online (bitswap, network and so on) | ||
*/ | ||
(cb) => node.goOnline(cb), | ||
/* | ||
* Add a file to IPFS - Complete Files API on: | ||
* https://github.com/ipfs/interface-ipfs-core/tree/master/API/files | ||
*/ | ||
(cb) => { | ||
if (node.isOnline()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the node is not online after doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not mandatory to be online to add the files. But get what you say, I basically preserved this operation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, sure, but in the previous step, we do go online, so either we're online here, or something bad happened. |
||
console.log('\nNode is now ready and online') | ||
} | ||
|
||
node.files.add(fileToAdd, (err, result) => { | ||
if (err) { return cb(err) } | ||
|
||
console.log('\nAdded file:') | ||
console.log(result[0]) | ||
fileMultihash = result[0].hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing mutation on the fileMultihash variable, you should be able to pass the value as a second parameter to the callback and then retrieve it in the next step, if I remember correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, I'm not a big fan of that, because then the tasks can't be easily swapped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, all right, makes sense 👍 |
||
cb() | ||
}) | ||
}, | ||
/* | ||
* Awesome we've added a file so let's retrieve and | ||
* display its contents from IPFS | ||
*/ | ||
(cb) => { | ||
node.files.cat(fileMultihash, (err, stream) => { | ||
if (err) { return cb(err) } | ||
|
||
console.log('\nFile content:') | ||
stream.pipe(process.stdout) | ||
stream.on('end', process.exit) | ||
}) | ||
} | ||
], (err) => { | ||
if (err) { | ||
return console.log(err) | ||
} | ||
console.log('Success!') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,8 @@ You should see the following: | |
|
||
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/1.png) | ||
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/2.png) | ||
|
||
## Special note | ||
|
||
In order to use js-ipfs in the browser, you need to replace the default `zlib` library by `browserify-zlib-next`, a full implementation of the native `zlib` package, full in Node.js. | ||
See the package.json to learn how to do this and avoid this pitfall. More context on: https://github.com/ipfs/js-ipfs#use-in-the-browser-with-browserify-webpack-or-any-bundler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "see the package.json" for what? I'm not sure but probably you mean "see the webpack.config.js" for aliasing zlib to browserify-zlib-next no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. absolutely! 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"stage": 0 | ||
"presets": [ | ||
"stage-0", | ||
"react" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're bundling this with webpack and stage-0 + react, files are already always running in strict mode. |
||
|
||
const React = require('react') | ||
const ReactDOM = require('react-dom') | ||
const App = require('./app') | ||
|
||
ReactDOM.render(<App />, document.getElementById('root')) |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"replace this by line below" should probably be expanded to "replace this by line below if you are running this example from outside the js-ipfs repository" just to be explicit about why you would replace it.