如何将我的内容发送到全局IPFS节点?
要在 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()
中;
请指导我:
我对 PowerShell 上的消息的理解正确吗?
如何在我的代码中使用全局 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:
Am I right about the messages on PowerShell?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
create()
时,客户端将连接到本地节点urlhttp:// localhost:5001
。要将另一个HTTP网关连接到全局节点,只需通过网关的URL即可。另请参阅: create> create options
When you call
create()
, the client connects to a local node the URLhttp://localhost:5001
. To connect to another HTTP gateway to a global node, simply pass the gateway's URL instead.See also:
create
options