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.
- Smart Contract Series 1: The Cornerstone of the Digital Society-Smart Contracts
- Technical Guide | Understanding Zk-stark for Zero Knowledge Proof Algorithms
- Popular science | Worry about privacy protection? Encrypted data warehouse shows its strength (core use cases and requirements analysis)
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!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Understanding Zk-stark of Zero Knowledge Proof Algorithm-Arithmetization
- Smart Contract Entry Series | Important Links in Smart Contract Engineering: Formal Verification Methods
- Technical Perspectives | After two years of developing DApps and Layer 2 networks, I switched to the Substrate camp
- Technology Viewpoint | Want to develop dApp with Wasm? You have to read the introductory tutorial (1)
- Technical Perspectives | Contract Upgrade for Python Smart Contract Tutorial
- How To | Build a Zero-Dependent Application on IPFS (Vol. 2)
- Technical Perspectives | Python Smart Contract Tutorial Native Contract Call