Technical Guide | Building IPFS Applications with Node.Js


Translation: Interstellar


IPFS is a game changer. In contrast to today's networks, IPFS can actually implement a distributed, permanent network. Most importantly, it may be interplanetary, which is completely impossible with our current client-server architecture. IPFS utilizes a peer-to-peer architecture where each node is both a client and a server. If you request something from the network, provide the content you requested to other nodes.

You can think of it as a huge BitTorrent group.

In addition to the point-to-point architecture, the way we retrieve content on IPFS is also revolutionary. Instead of identifying content by its location, that is, its IP address, we identify it by a unique identifier created by hashing the content itself . This is called content addressing. This is useful for several reasons:

  • Data persistence . If many peers have what you want, you will get from your closest peers. Even if the original uploader of the content is offline, you will most likely still get it because it is addressable by content. Compared to today's Web, if a server goes down, the content is reduced, which is a big improvement.
  • Built-in security . Because the content identifier (CID) is created by hashing the content itself, you can always ensure that you get exactly what you need. The system cannot be tricked because if you modify the content, its identifier will change.

getting Started

Before I start, I want to solve some problems. There are two implementations of the IPFS client, one in JavaScript and the other in Go. JavaScript seems to be the best choice here, but because it is in an earlier development state than the Go client, this is not the best choice. We will use the Go client and connect with Node through its API. prerequisites:

  • Node.js (preferably one of the later versions) and npm
  • go-ipfs client (installation instructions here)
  • Postman or curl to make requests to our REST API

First, our node must be running in online mode, so open a terminal and run the ipfs daemon. You should see the following:

On line 19, you can see that the API server is listening on port 5001. This is the port we need to connect to.

Second, create a new project directory and run npm install ipfs-http-client. This is the package we need to connect to the running IPFS node. Next, let's create a js file in which we will connect to the nodes:

On line 3, we are actually connecting to the daemon API server. Now we can start executing commands on the ipfs object to interact with the network.

As an example, let's write a function that adds some text to IPFS:

On line 1, we create an object to be added to IPFS. This path is the content of the file we want to call on IPFS (we can include the directory), and the content is the content of the file we want to add to the Buffer (plain text in this example). Next, we use ipfs.add () to add the file to ipfs, which will return an array containing all the added files. Since we have only added one, the result console.log () will be:

If you proceed, you will find that the hash field will be exactly the same every time because you added the same content as me. Also, please note that the path name does not affect the content identifier. If you want to retrieve content now, there are two possibilities: Use a local gateway server like this: http: // localhost: 8080 / ipfs / QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX Your IPFS daemon has started the gateway server. Use a public gateway like this: All of this is cool, but let's see how to use it in an application. To demonstrate more features, let's use Express to create a small REST API.

Create application

Don't forget to run npm install express. Let's start with some boilerplate code:

Go ahead and run it and use curl http: // localhost: 3000 for testing. You should see Welcome to my IPFS app.

Now add a POST route:

Now we can test this route with the postman. Create a new POST request http: // localhost: 3000 / upload. You can choose what to put in the body, but it must be JSON:

If all goes well, you should have received my reply and some terminal output from the Express application: {path: 'postman request', content: 'postman says whassup'}. Since we use JSON middleware, req.body is parsed into JS objects, which is already the format we need to add content to IPFS. Let's extend the addFile function by modifying and calling the function, and then return the link to the added file through the public gateway:

On line 8 of addFile (), we take the req.body data as a parameter and use it to increase IPFS. Then fileHash, we return to include it in the link sent back as a response. If we now make another POST request to the postman:

We return a link to view our files on the public gateway! Please note that since public gateways can be slow, it may actually take some time to load this link.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!


Was this article helpful?

93 out of 132 found this helpful

Discover more


Encryption settlement day in Sweden? Central Bank urges clarification of fiat currency

The oldest central bank on the planet called on politicians to legally define the concept of money, which may include...


There are 5 million BTCs lost worldwide. Is this the reason why BTC prices can rise further?

With the gradual recovery of the market, many newcomers have entered the market. Before the heroes explained some bas...


U.S. $ 6 trillion economic stimulus plan is immediate, Bitcoin may become the biggest winner

In the past few weeks, the entire world, including the Bitcoin industry, has been disrupted by the outbreak of the co...


Analysis: For the first time in 18 years, bitcoin prices are higher than the main moving average for the first time. What is the signal?

  What to watch today–the price of Bitcoin is higher than the main moving average for the first time. Last...


Miss Japan's most beautiful bitcoin: I am chasing madly and constantly learning because I do n’t want to give up

"8 Questions" an interview with Japan ’s Big V Miss Bitcoin, the feature film will be launched on Ba...


brc20-swap goes live Explaining the development process, product model, and future expectations

This article by LianGuaiNews will provide you with a detailed explanation of the development process, product model, ...