Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ This project is available through [npm](https://www.npmjs.com/). To install run
> npm install ipfs --save
```

Requires npm@3 and node@6 or above, tested on OSX & Linux, expected to work on Windows.
We support both the Current and Active LTS versions of Node.js. Please see [nodejs.org](https://nodejs.org/) for what these currently are.

This project is tested on OSX & Linux, expected to work on Windows.

### Use in Node.js

Expand Down
38 changes: 14 additions & 24 deletions examples/ipfs-101/1.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
'use strict'

const series = require('async/series')
const IPFS = require('ipfs')

const node = new IPFS()
let fileMultihash

series([
(cb) => node.on('ready', cb),
(cb) => node.version((err, version) => {
if (err) { return cb(err) }
console.log('Version:', version.version)
cb()
}),
(cb) => node.files.add({

node.on('ready', async () => {
const version = await node.version()

console.log('Version:', version.version)

const filesAdded = await node.files.add({
path: 'hello.txt',
content: Buffer.from('Hello World 101')
}, (err, filesAdded) => {
if (err) { return cb(err) }

console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
fileMultihash = filesAdded[0].hash
cb()
}),
(cb) => node.files.cat(fileMultihash, (err, data) => {
if (err) { return cb(err) }

console.log('\nFile content:')
process.stdout.write(data)
})
])

console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)

const fileBuffer = await node.files.cat(filesAdded[0].hash)

console.log('Added file contents:', fileBuffer.toString())
})
89 changes: 50 additions & 39 deletions examples/ipfs-101/README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,91 @@
# IPFS 101, spawn a node and add a file to the IPFS network

In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and throught the gateway.
In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and through the gateway.

You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install the following dependencies: `ipfs` and `async` using `npm install ipfs async`.
You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install `ipfs` using `npm install ipfs`.

Creating an IPFS instance can be done in one line, after requiring the module, you simply have to:

```JavaScript
```js
const IPFS = require('ipfs')

const node = new IPFS()
```

We can listen for the `ready` event to learn when the node is ready to be used. In this part, we start using `async/series` to help us manage the async flow. As a test, we are going to check the version of the node.
We can listen for the `ready` event to learn when the node is ready to be used. Within the ready event, we'll use `async`/`await` to help us manage the async flow.

```JavaScript
As a test, we are going to check the version of the node.

```js
const IPFS = require('ipfs')

const node = new IPFS()

series([
(cb) => node.on('ready', cb),
(cb) => node.version((err, version) => {
if (err) { return cb(err) }
console.log('Version:', version.version)
cb()
})
])
node.on('ready', async () => {
const version = await node.version()

console.log('Version:', version.version)
})
```

(If you prefer not to use `async`/`await`, you can instead use `.then()` as you would with any promise,
or pass an [error-first callback](https://nodejs.org/api/errors.html#errors_error_first_callbacks), e.g. `node.version((err, version) => { ... })`)

Running the code above gets you:

```bash
> node 1.js
IPFS Version: 0.25.0
Version: 0.31.2
```

Now lets make it more interesting and add a file to IPFS. We can do it by adding another async call to the series that uses the `node.files.add` call. You can learn about IPFS API for files at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md).

```JavaScript
// Create the File to add, a file consists of a path + content. More details on
// https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md
(cb) => node.files.add({
path: 'hello.txt',
content: Buffer.from('Hello World')
}, (err, filesAdded) => {
if (err) { return cb(err) }

// Once the file is added, we get back an object containing the path, the
// multihash and the sie of the file
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
fileMultihash = filesAdded[0].hash
cb()
Now let's make it more interesting and add a file to IPFS using `node.files.add`. A file consists of a path and content.

You can learn about the IPFS File API at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md).

```js
node.on('ready', async () => {
const version = await node.version()

console.log('Version:', version.version)

const filesAdded = await node.files.add({
path: 'hello.txt',
content: Buffer.from('Hello World 101')
})

console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)
})
```

If you avoid calling that last `cb()`, the program won't exit enabling you to go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it!
You can now go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it!

```bash
> node 1.js
Version: 0.25.0
Version: 0.31.2

Added file: hello.txt QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
# Copy that hash and load it on the gateway, here is a prefiled url:
# https://ipfs.io/ipfs/QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
```

The last step of this tutorial is retrieving the file back using the `cat` 😺 call. Add another step on the series chain that does the following:
The last step of this tutorial is retrieving the file back using the `cat` 😺 call.

```js
node.on('ready', async () => {
const version = await node.version()

console.log('Version:', version.version)

const filesAdded = await node.files.add({
path: 'hello.txt',
content: Buffer.from('Hello World 101')
})

console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)

```JavaScript
(cb) => node.files.cat(fileMultihash, (err, data) => {
if (err) { return cb(err) }
const fileBuffer = await node.files.cat(filesAdded[0].hash)

console.log('\nFile content:')
// print the file to the terminal and then exit the program
process.stdout.write(data)
console.log('Added file contents:', fileBuffer.toString())
})
```

Expand Down
1 change: 0 additions & 1 deletion examples/ipfs-101/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"ipfs": "file:../../"
}
}