-
-
Save milkyklim/32f2a000f55a34b85333a710356a08b2 to your computer and use it in GitHub Desktop.
IPFS backend to add and get file from IPFS system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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