getting error on ipfs while generating hash using node js

Viewed 24

I am trying to upload file on ipfs and getting hash from there. When I hit the API I got error

HTTPError: File argument 'path' is required

const ipfsClient = require('ipfs-http-client');
const express = require('express');
const ipfs =  ipfsClient('http://localhost:5001')
const fs= require('fs');
// const hash =  require('hash')
const app  = express();
app.use(express.json());

app.get('/', (req, res)=> { 
    return res.send('welcome to my ipfs')
})

app.post('/upload', async (req, res)=> { 
    const data =  req.body;
    console.log("---data",  data);
    const fileHash =  await addFile(data)
    console.log("=======fileHash", fileHash);
    return res.send(`http://localhost:8080/ipfs/${ fileHash }`)
})

const  addFile = async ({path , content})=> { 
        const file = { path: path, content : Buffer.from(content)};
        const filesAdded =  await ipfs.add(file);   
        for await (const result of filesAdded) {
            if (result.hash) {
              hashes.push(result.hash);
            } else if (result.cid) {
              hashes.push(result.cid.multihash); // guessing here - might be another part of the cid object
            } else {
              console.log("No hash found in",result);
            }
          }
        
          return hashes[0]; // if you know it's just one for example
        };
  

and getting error

/node_modules/ipfs-http-client/src/lib/error-handler.js:41   const
error = new HTTPError(response)
                ^

HTTPError: File argument 'path' is required
0 Answers
Related