Demo of "Using go-ipfs as a library" with js-ipfs
Explore the docs
·
View Demo
·
Report Bug
·
Request Feature/Example
- Table of Contents
- About The Project
- Getting Started
- Usage
- References
- Documentation
- Contributing
- Want to hack on IPFS?
- Read the docs
- Look into other examples to learn how to spawn an IPFS node in Node.js and in the Browser
- Consult the Core API docs to see what you can do with an IPFS node
- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
- Check out https://docs.ipfs.io for tips, how-tos and more
- See https://blog.ipfs.io for news and more
- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io
Make sure you have installed all of the following prerequisites on your development machine:
- Git - Download & Install Git. OSX and Linux machines typically have this already installed.
- Node.js - Download & Install Node.js and the npm package manager.
> npm install
> npm start
This tutorial is the sibling of the go-ipfs "Using go-ipfs as a library" tutorial.
In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and through the gateway.
You can find a complete version of this tutorial in 1.js. For this tutorial, you need to install ipfs
using npm install ipfs
.
Creating an IPFS instance can be done in one line, after requiring the module, you simply have to:
import * as IPFS from 'ipfs-core';
async function main() {
const node = await IPFS.create();
// ...
}
main();
As a test, we are going to check the version of the node.
import * as IPFS from 'ipfs-core';
async function main() {
const node = await IPFS.create();
const version = await node.version();
console.log("Version:", version.version);
// ...
}
main();
(If you prefer not to use async
/await
, you can instead use .then()
as you would with any promise, or pass an error-first callback, e.g. node.version((err, version) => { ... })
)
Running the code above gets you:
> node 1.js
Version: 0.31.2
Now let's make it more interesting and add a file to IPFS using node.add
. A file consists of a path and content.
You can learn about the IPFS File API at interface-ipfs-core.
import * as IPFS from 'ipfs-core';
async function main() {
const node = await IPFS.create();
const version = await node.version();
console.log("Version:", version.version);
const fileAdded = await node.add({
path: "hello.txt",
content: "Hello World 101",
});
console.log("Added file:", fileAdded.path, fileAdded.cid);
// ...
}
main();
You can now go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it!
> node 1.js
Version: 0.31.2
Added file: hello.txt QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
# Copy that hash and load it on the gateway, here is a prefiled url:
# https://ipfs.io/ipfs/QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
The last step of this tutorial is retrieving the file back using the cat
😺 call.
import * as IPFS from 'ipfs-core';
async function main() {
const node = await IPFS.create();
const version = await node.version();
console.log("Version:", version.version);
const fileAdded = await node.add({
path: "hello.txt",
content: "Hello World 101",
});
console.log("Added file:", fileAdded.path, fileAdded.cid);
const decoder = new TextDecoder()
let text = ''
for await (const chunk of node.cat(fileAdded.cid)) {
text += decoder.decode(chunk, {
stream: true
})
}
console.log("Added file contents:", text);
}
main();
That's it! You just added and retrieved a file from the Distributed Web!
For more examples, please refer to the Documentation
- Documentation:
- Tutorials:
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the IPFS Project
- Create your Feature Branch (
git checkout -b feature/amazing-feature
) - Commit your Changes (
git commit -a -m 'feat: add some amazing feature'
) - Push to the Branch (
git push origin feature/amazing-feature
) - Open a Pull Request
The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:
Read the Code of Conduct and JavaScript Contributing Guidelines.
- Check out existing issues The issue list has many that are marked as 'help wanted' or 'difficulty:easy' which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
- Look at the IPFS Roadmap This are the high priority items being worked on right now
- Perform code reviews More eyes will help a. speed the project along b. ensure quality, and c. reduce possible future bugs.
- Add tests. There can never be enough tests.
- Join the Weekly Core Implementations Call it's where everyone discusses what's going on with IPFS and what's next