How To Get Started With IPFS and Node - Better Programming

August 08, 2019 0 Comments

How To Get Started With IPFS and Node - Better Programming

 

 

To get started we’re going to need Node.js installed and a text editor.

Prerequisites

Now that we have Node installed, let’s get cooking. Start with creating a new Node project.

npm init -y

Now we have the package.json file which lets us install node modules. Go ahead by and install the ipfs node module.

npm install ipfs

Good job! Next, we need to create a index.js file, in which we’ll place our application logic.

touch index.js

Inside index.js place the following logic.

We simply import the IPFS module and initially our node application with the main() function.

Now we’ve written the boilerplate code, it’s time to implement the IPFS code itself.

Let’s spawn an IPFS node. It will look like this:

If you run this code with Node you should see your console alerting you with messages regarding the IPFS:

node index.js
Spawning IPFS nodes

Excellent! This means everything is working as expected. If you're not seeing this, try installing the ipfs package again.

Since we spawned an IPFS node, let’s put it to use. Start by adding a file to the IPFS node. Once we add the node, we can retrieve the file back using the cat call.

Voila! Check the console for the fileBuffer.

That's it! You just added and retrieved a file from the Distributed Web. Find the source code here.

We barely scratched the surface here — if you’re curious to learn more, here’s a full repository of more examples.


Tag cloud