【问题标题】:Not able to see ipfs node id无法看到 ipfs 节点 ID
【发布时间】:2018-08-04 05:39:53
【问题描述】:

下面是我的 app.js 文件。当我运行以下代码时,它显示“群监听”和“文件路径”,然后什么也没有发生。 我还在 5001 上的另一个命令提示符监听 api 上运行守护进程。 我认为节点没有被启动,这就是它永远不会准备好的原因。

var express = require('express');
var app = express();
var ipfsAPI = require('ipfs-api')
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}) 
const IPFS = require('ipfs');
// Spawn your IPFS node \o/
const node = new IPFS();

var multer  = require('multer');
var upload = multer({ dest: './z000000000'});
var fs = require('fs');

/** Permissible loading a single file, 
the value of the attribute "name" in the form of "recfile". **/
var type = upload.single('filer');

app.post('/upload', type, function (req,res) {

/** When using the "single"
  data come in "req.file" regardless of the attribute "name". **/
 var tmp_path = req.file.path;
 console.log(tmp_path);
  //
node.on('ready', () => {
node.id((err, id) => {
    if (err) {
        return console.log(err)
    }
    console.log(id)
})

var data = fs.readFileSync('./'+tmp_path);
console.log("Synchronous read: " + data.toString());

let files = [
    {
        path: './'+tmp_path,
        content: data
    }
]
node.files.add(files, function (err, files) {
    if (err) {
        console.log(err);
    } else {
        console.log(files)
    }
})
})
//
 res.end();
});



var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})

【问题讨论】:

    标签: node.js daemon ipfs


    【解决方案1】:

    使用 ipfsAPI:

    var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}

    获取节点ID:

    ipfs.id()
        .then(res => {
          showStatus(`daemon active\nid: ${res.ID}`, COLORS.success)
          doYourWork()
        })
        .catch(err => {
          showStatus('daemon inactive', COLORS.error)
          console.error(err)
        })
    

    在此处查找文档:https://github.com/ipfs/js-ipfs-api

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 2016-12-24
      • 2021-02-19
      • 2016-03-06
      • 1970-01-01
      • 2022-06-30
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多