0

下面是我的 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)
})
4

1 回答 1

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

于 2018-02-24T07:30:16.333 回答