Skip to content

Instantly share code, notes, and snippets.

@milkyklim
Forked from sulejirl/ipfs-backend.js
Last active July 21, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milkyklim/32f2a000f55a34b85333a710356a08b2 to your computer and use it in GitHub Desktop.
Save milkyklim/32f2a000f55a34b85333a710356a08b2 to your computer and use it in GitHub Desktop.
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