无法看到ipfs节点ID

时间:2018-02-24 06:48:04

标签: node.js daemon ipfs

下面是我的app.js文件。当我运行以下代码时,它显示" swarm listen on"和"文件路径"然后没有任何反应。 我也在另一个命令提示符上运行守护进程,在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)
})

1 个答案:

答案 0 :(得分:0)

使用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