如何将我的内容发送到全局IPFS节点?

发布于 2025-01-18 00:54:34 字数 2070 浏览 0 评论 0原文

要在 NodeJS 中使用 IPFS,我需要 ipfs 模块。要连接 IPFS 节点,我尝试:

const IPFS=require("ipfs");
const node= await IPFS.create();

之后,当我在 PowerShell 中运行 NodeJS 时,会出现以下消息:

Swarm listening on /ip4/192.168.1.100/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.88.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.49.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp

我认为上述消息提到我连接到本地 IPFS 节点。如果我的想法是正确的,那可能是因为 IPFS.create()。因此,我应该将网关作为连接全局节点的参数。我在 公共网关检查器 上看到了一些 IPFS 公共网关,但我不知道如何将一个样本放入 IPFS.create() 中;

请指导我:

  1. 我对 PowerShell 上的消息的理解正确吗?

  2. 如何在我的代码中使用全局 IPFS 网关?

我的整个 NodeJS 代码:

const IPFS=require("ipfs");
    async function register(){
        const node=await IPFS.create();
        node.add("Main Content!").then(function(ipfsHash){console.log(ipfsHash)});
    }

PowerShell 中的整个结果:

Swarm listening on /ip4/192.168.1.100/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.88.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.49.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
{
  path: 'QmUaTPt2Uus46Cgo4YMKze8PpHrCKvZ1YXD2zpffNt8U9S',
  cid: CID(QmUaTPt2Uus46Cgo4YMKze8PpHrCKvZ1YXD2zpffNt8U9S),
  size: 21,
  mode: 420,
  mtime: undefined
}

非常感谢!

To use IPFS in NodeJS, I require ipfs module. To connect a IPFS node I try:

const IPFS=require("ipfs");
const node= await IPFS.create();

After that, When I run NodeJS in PowerShell, The below messages emerge:

Swarm listening on /ip4/192.168.1.100/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.88.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.49.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp

I think the above messages are mentioning that I connected to a local IPFS node. If my thought is true, It can be because of IPFS.create(). So, I should put a gateway as an argument to that to connecting a global node. I saw some IPFS public gateways on Public Gateway Checker but I don't know how to put one sample in IPFS.create();

Please giude me:

  1. Am I right about the messages on PowerShell?

  2. How to use a global IPFS gateway in my code?

My whole NodeJS code:

const IPFS=require("ipfs");
    async function register(){
        const node=await IPFS.create();
        node.add("Main Content!").then(function(ipfsHash){console.log(ipfsHash)});
    }

Whole result in PowerShell:

Swarm listening on /ip4/192.168.1.100/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.88.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/192.168.49.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/12D3KooWCGvmbySrpdCVKACJPjZeeUiMR6gSQffa7W3gWZLSGBxp
{
  path: 'QmUaTPt2Uus46Cgo4YMKze8PpHrCKvZ1YXD2zpffNt8U9S',
  cid: CID(QmUaTPt2Uus46Cgo4YMKze8PpHrCKvZ1YXD2zpffNt8U9S),
  size: 21,
  mode: 420,
  mtime: undefined
}

Thank you a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2025-01-25 00:54:34

调用create()时,客户端将连接到本地节点url http:// localhost:5001。要将另一个HTTP网关连接到全局节点,只需通过网关的URL即可。

const client = await IPFS.create("https://<your-preferred-gateway>");
const { cid } = await client.add("Your content");

另请参阅: create> create options

When you call create(), the client connects to a local node the URL http://localhost:5001. To connect to another HTTP gateway to a global node, simply pass the gateway's URL instead.

const client = await IPFS.create("https://<your-preferred-gateway>");
const { cid } = await client.add("Your content");

See also: create options

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文