Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IPFS backend to add and get file from IPFS system
const IPFS = require('ipfs');
const express = require('express');
const fs = require('fs');
const app = express();
const ipfs = new IPFS();
app.get('/addfile', () => {
// reading file from computer
let file = fs.readFileSync('test.png');
// creating buffer for ipfs function
let buffer = new Buffer.from(file);
ipfs.add(buffer, (err, file) => {
if (err)
return console.log(err);
console.log(file);
})
})
app.get('/getfile', () => {
// this hash is returned hash in addfile
const hash = 'QmU7VWfd3DN1Hh8fjALhQyJLgtkwxkYP2zz9MDT4rkyVJ1'
ipfs.get(hash, (err, files) => {
if (err)
return console.log(err);
files.forEach((file) => {
fs.writeFile(file.path + '.png', file.content, 'binary', (err) => {
if(err)
return console.log(err);
console.log('The file was saved!');
})
})
})
})
app.listen(3000, () => console.log('App listening on port 3000!'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.