存储 DApp 资源
学习使用 IPFS 上传和获取 DApp 前端资源、NFT 元数据及其他文件,实现去中心化。
存储 DApp 资源 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Decentralize DApp Assets?
Traditional web applications host their front-ends (like HTML, CSS, and JavaScript files) on centralized servers. This means if that server goes down or is censored, the application becomes inaccessible.
Decentralized Applications (DApps) aim for full decentralization, but if your smart contract is on a blockchain while your user interface (UI) is on a single cloud server, is it truly decentralized? This lesson explores how IPFS helps.
Hosting Your DApp's UI on IPFS
IPFS provides a robust solution for hosting your entire DApp front-end. This includes all your static files: HTML pages, CSS stylesheets, JavaScript code, and images.
- Once uploaded, the entire DApp content (or individual files) receives a unique Content Identifier (CID).
- Users can then access your DApp directly via an IPFS gateway URL, making it highly censorship-resistant and always available, as long as it's pinned on the network.
CIDs: Content's Digital Fingerprint
As you learned in the previous lesson, IPFS uses Content Identifiers (CIDs) instead of location-based addresses.
- A CID is a cryptographic hash of the content itself. This means if even a single byte of the content changes, its CID will also change.
- This content addressing ensures data integrity and immutability. You can always verify that you are accessing the exact file you expect, free from tampering.
Uploading Assets to IPFS
To place your files onto the IPFS network, you typically use the IPFS command-line interface (CLI) or a client library in your code.
The ipfs add command is commonly used. It takes a file or a directory and returns its unique CID.
For example, to upload a folder containing your DApp's front-end:
ipfs add my_dapp_folder/This command would process your folder and output a CID that represents its entire content.
Ensuring Data Stays Available (Pinning)
When you use ipfs add, your content is initially stored only on your local IPFS node. For others to reliably access it, that content needs to be "pinned."
- Pinning is the process of telling an IPFS node to keep a copy of specific content indefinitely, preventing it from being garbage collected.
- You can pin content to your own IPFS node, or more commonly for DApps, use a third-party "pinning service."
- Pinning services provide dedicated infrastructure to host your content, ensuring high availability and uptime for your DApp's assets.
Decentralizing NFT Metadata
Non-Fungible Tokens (NFTs) often represent unique digital assets like images, videos, or audio. The NFT smart contract itself doesn't store the asset directly.
Instead, the smart contract typically stores a URI (Uniform Resource Identifier) that points to the asset's metadata. Storing this metadata (and often the asset itself) on IPFS is crucial:
- It prevents the metadata from being altered or disappearing if a centralized server hosting it goes offline.
- It ensures the NFT's value is tied to a persistent, verifiable piece of data.
Anatomy of NFT Metadata (JSON)
NFT metadata is typically a JSON (JavaScript Object Notation) file that provides descriptive information about the digital asset.
It usually includes details such as the asset's name, description, and a link to the actual media file (which is often also stored on IPFS).
Here's a common example structure:
{
"name": "My Awesome NFT",
"description": "A unique digital collectible.",
"image": "ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26h7y3g7o...",
"attributes": [
{ "trait_type": "Background", "value": "Blue" }
]
}The image field, for instance, uses an ipfs:// URI scheme to point directly to the asset's CID on IPFS.
How Users Access IPFS Content
Most end-users don't run their own IPFS nodes. So, how do they access content stored on IPFS?
- They access it through "IPFS Gateways."
- An IPFS gateway is a regular web server that acts as a bridge. It fetches content from the IPFS network on demand and serves it over standard HTTP to web browsers.
- Popular public gateways include
https://cloudflare-ipfs.com/ipfs/<your-CID>orhttps://ipfs.io/ipfs/<your-CID>.
DApps Retrieving IPFS Data
Your DApp's front-end code (typically written in JavaScript) can easily retrieve content from IPFS using CIDs and gateways.
- The DApp constructs a standard web URL using the IPFS gateway address and the content's CID.
- It then uses standard web requests (like the JavaScript
fetchAPI) to retrieve the content, just like any other web resource.
Here's a conceptual JavaScript example:
const cid = "bafybeigdyrzt5sfp7udm7hu...";
const gatewayUrl = `https://ipfs.io/ipfs/${cid}`;
fetch(gatewayUrl)
.then(response => response.json())
.then(data => console.log("NFT Metadata:", data))
.catch(error => console.error("Error fetching data:", error));IPFS Storage Check
When building a DApp, why is it beneficial to store your front-end assets (like HTML, CSS, JavaScript) and NFT metadata on IPFS instead of a traditional centralized web server?
Recap: Storing DApp Assets
You've successfully learned how IPFS provides a decentralized and robust way to store critical DApp components, including front-end assets and NFT metadata.
- We covered the practical aspects of uploading files using
ipfs addand the crucial role of pinning services for content persistence. - You now understand how CIDs ensure immutability and how IPFS gateways enable users to access this decentralized content via standard web browsers.
- This knowledge is fundamental to building DApps that are resilient, censorship-resistant, and truly decentralized.
Next, we'll explore Filecoin, which builds upon IPFS to offer an incentivized and verifiable layer for long-term, persistent storage solutions.
常见问题解答
「存储 DApp 资源」课时是免费的吗?
是的 — 「存储 DApp 资源」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。
「存储 DApp 资源」这节课中我会学到什么?
学习使用 IPFS 上传和获取 DApp 前端资源、NFT 元数据及其他文件,实现去中心化。 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「存储 DApp 资源」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- IPFS 入门
- 存储 DApp 资源
- Filecoin 与去中心化存储