当使用node.js应用程序中的以下代码以编程方式启动 js-ipfs 节点时,它会启动 swarm,允许添加文件并查询它们。
// code from the docs: https://github.com/ipfs/js-ipfs#use-in-nodejs
const IPFS = require('ipfs')
const node = new IPFS()
node.on('ready', () => {
// Ready to use!
})
Run Code Online (Sandbox Code Playgroud)
但 API 和网关不可用,这意味着 Web-ui 无法检查存储库内容。如何使用 npm 包启动 API 网关以及 ipfs swarm ipfs?
找到答案,发布在这里以帮助任何寻找类似信息的人。
API 网关作为http内的模块提供ipfs,可以在 ipfs 节点启动时调用,如下所示:
const IPFS = require('ipfs')
const node = new IPFS()
node.on('ready', () => {
// start the API gateway
const Gateway = require('ipfs/src/http');
const gateway = new Gateway(node);
return gateway.start();
})
Run Code Online (Sandbox Code Playgroud)
API 和网关将侦听 中使用的配置中指定的端口new IPFS(),该端口可以从文件位置编辑repo/config或以编程方式提供,例如:
"Addresses": {
"API": "/ip4/127.0.0.1/tcp/5001",
"Gateway": "/ip4/127.0.0.1/tcp/8080"
}
Run Code Online (Sandbox Code Playgroud)