|
1 | 1 | # IPFS 101, spawn a node and add a file to the IPFS network
|
2 | 2 |
|
3 |
| -In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and throught the gateway. |
| 3 | +In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and through the gateway. |
4 | 4 |
|
5 |
| -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`. |
| 5 | +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`. |
6 | 6 |
|
7 | 7 | Creating an IPFS instance can be done in one line, after requiring the module, you simply have to:
|
8 | 8 |
|
9 |
| -```JavaScript |
| 9 | +```js |
10 | 10 | const IPFS = require('ipfs')
|
11 | 11 |
|
12 | 12 | const node = new IPFS()
|
13 | 13 | ```
|
14 | 14 |
|
15 |
| -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. |
| 15 | +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. |
16 | 16 |
|
17 |
| -```JavaScript |
| 17 | +As a test, we are going to check the version of the node. |
| 18 | + |
| 19 | +```js |
18 | 20 | const IPFS = require('ipfs')
|
19 | 21 |
|
20 | 22 | const node = new IPFS()
|
21 | 23 |
|
22 |
| -series([ |
23 |
| - (cb) => node.on('ready', cb), |
24 |
| - (cb) => node.version((err, version) => { |
25 |
| - if (err) { return cb(err) } |
26 |
| - console.log('Version:', version.version) |
27 |
| - cb() |
28 |
| - }) |
29 |
| -]) |
| 24 | +node.on('ready', async () => { |
| 25 | + const version = await node.version() |
| 26 | + |
| 27 | + console.log('Version:', version.version) |
| 28 | +}) |
30 | 29 | ```
|
31 | 30 |
|
| 31 | +(If you prefer not to use `async`/`await`, you can instead use `.then()` as you would with any promise, |
| 32 | +or pass an [error-first callback](https://nodejs.org/api/errors.html#errors_error_first_callbacks), e.g. `node.version((err, version) => { ... })`) |
| 33 | + |
32 | 34 | Running the code above gets you:
|
33 | 35 |
|
34 | 36 | ```bash
|
35 | 37 | > node 1.js
|
36 |
| -IPFS Version: 0.25.0 |
| 38 | +Version: 0.31.2 |
37 | 39 | ```
|
38 | 40 |
|
39 |
| -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). |
40 |
| - |
41 |
| -```JavaScript |
42 |
| -// Create the File to add, a file consists of a path + content. More details on |
43 |
| -// https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md |
44 |
| -(cb) => node.files.add({ |
45 |
| - path: 'hello.txt', |
46 |
| - content: Buffer.from('Hello World') |
47 |
| -}, (err, filesAdded) => { |
48 |
| - if (err) { return cb(err) } |
49 |
| - |
50 |
| - // Once the file is added, we get back an object containing the path, the |
51 |
| - // multihash and the sie of the file |
52 |
| - console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash) |
53 |
| - fileMultihash = filesAdded[0].hash |
54 |
| - cb() |
| 41 | +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. |
| 42 | + |
| 43 | +You can learn about the IPFS File API at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md). |
| 44 | + |
| 45 | +```js |
| 46 | +node.on('ready', async () => { |
| 47 | + const version = await node.version() |
| 48 | + |
| 49 | + console.log('Version:', version.version) |
| 50 | + |
| 51 | + const filesAdded = await node.files.add({ |
| 52 | + path: 'hello.txt', |
| 53 | + content: Buffer.from('Hello World 101') |
| 54 | + }) |
| 55 | + |
| 56 | + console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) |
55 | 57 | })
|
56 | 58 | ```
|
57 | 59 |
|
58 |
| -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! |
| 60 | +You can now go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it! |
59 | 61 |
|
60 | 62 | ```bash
|
61 | 63 | > node 1.js
|
62 |
| -Version: 0.25.0 |
| 64 | +Version: 0.31.2 |
63 | 65 |
|
64 | 66 | Added file: hello.txt QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
|
65 | 67 | # Copy that hash and load it on the gateway, here is a prefiled url:
|
66 | 68 | # https://ipfs.io/ipfs/QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
|
67 | 69 | ```
|
68 | 70 |
|
69 |
| -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: |
| 71 | +The last step of this tutorial is retrieving the file back using the `cat` 😺 call. |
| 72 | + |
| 73 | +```js |
| 74 | +node.on('ready', async () => { |
| 75 | + const version = await node.version() |
| 76 | + |
| 77 | + console.log('Version:', version.version) |
| 78 | + |
| 79 | + const filesAdded = await node.files.add({ |
| 80 | + path: 'hello.txt', |
| 81 | + content: Buffer.from('Hello World 101') |
| 82 | + }) |
| 83 | + |
| 84 | + console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) |
70 | 85 |
|
71 |
| -```JavaScript |
72 |
| -(cb) => node.files.cat(fileMultihash, (err, data) => { |
73 |
| - if (err) { return cb(err) } |
| 86 | + const fileBuffer = await node.files.cat(filesAdded[0].hash) |
74 | 87 |
|
75 |
| - console.log('\nFile content:') |
76 |
| - // print the file to the terminal and then exit the program |
77 |
| - process.stdout.write(data) |
| 88 | + console.log('Added file contents:', fileBuffer.toString()) |
78 | 89 | })
|
79 | 90 | ```
|
80 | 91 |
|
|
0 commit comments