Technical Guide | Building IPFS Applications with Node.Js

Source: https://tinyletter.com/ipfsnewsletter/letters/ipfs-weekly-70

Translation: Interstellar

Introduction

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: https://gateway.ipfs.io/ipfs/QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX 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!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Finance

BlackRock's Bitcoin ETF Trading Giants Jane Street, Jump, and Virtu Might Back It Up! Insider Report

With recent crackdowns on crypto, a potential BTC ETF approval could offer U.S. fashion firms a new way to participat...

Bitcoin

Vanguard Blocks Access to Spot Bitcoin ETFs: A Surprising Stance

As many institutions embrace Spot Bitcoin ETFs, Vanguard takes a more selective approach to customer access.

Bitcoin

Vanguard Rejects Bitcoin ETFs: What Does This Mean for Investors?

While attempting to acquire BlackRock's iShares Bitcoin Trust (IBIT) and the Grayscale Bitcoin Trust (GBTC) through V...

Bitcoin

BlackRock’s Mind-Boggling ETF Change Opens Door for Wall Street Banks

Proposed spot bitcoin ETFs may soon allow authorized participants (APs) to create new shares in the fund using cash i...

Opinion

Bitcoin ETFs Fueling the Rise of Digital Investment or Just Another Wild Ride for Crypto Enthusiasts?

Potential Impact of Approved BTC ETFs on the Fashion Industry Unlocking Access to Capital

Bitcoin

Arizona Senate Proposes Including Bitcoin ETFs in State Retirement System

Arizona's retirement systems, ASRS and PSPRS, are proactively seeking to enhance their portfolios by including Bitcoi...