Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
I am saving an IPFS file and using res.send() to push it to the front end, why is this not working?

The error tells me i "Cannot set headers after they are sent to the client"

async function saveFile(file) {
    await ipfs.add(file, (err, result) => {
        if (err){
            console.log(err);
        }
        console.log(result);
    });
}
app.post('/upload', async (req, res) => {
    const file = req.files.file;
    const fileName = req.body.fileName;
    const filePath = 'files/' + fileName;
    console.log(file);
    const fileHash = saveFile(JSON.stringify(file));
    res.send(fileHash);
    res.render('pages/upload.ejs', { fileName, fileHash });
    
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm having trouble finding the documentation of ipfs.add() in the js-ipfs documentation, so the code below is a guess.

When you use await with an async function, you don't generally provide a callback. The value will be returned by await, and errors are turned into exceptions, so I use try/catch around it.

saveFile() needs to return the file hash returned by ipfs.add(), and you need to use await when calling saveFile to get the returned value.

async function saveFile(file) {
  try {
    let result = await ipfs.add(file);
    console.log(result);
    return result;
  } catch (err) {
    console.log(err);
  }
}
app.post('/upload', async(req, res) => {
  const file = req.files.file;
  const fileName = req.body.fileName;
  const filePath = 'files/' + fileName;
  console.log(file);
  const fileHash = await saveFile(JSON.stringify(file));
  res.send(fileHash);
  res.render('pages/upload.ejs', {
    fileName,
    fileHash
  });
});

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!