Last active
January 12, 2023 15:34
-
-
Save sulejirl/14e63ca5e6fb7c70210fbd659629b66b to your computer and use it in GitHub Desktop.
IPFS backend for sending and getting file from the ipfs system without deploying local node
This file contains hidden or 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
//Required modules | |
const ipfsAPI = require('ipfs-api'); | |
const express = require('express'); | |
const fs = require('fs'); | |
const app = express(); | |
//Connceting to the ipfs network via infura gateway | |
const ipfs = ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'}) | |
//Reading file from computer | |
let testFile = fs.readFileSync("PATH_OF_FILE"); | |
//Creating buffer for ipfs function to add file to the system | |
let testBuffer = new Buffer(testFile); | |
//Addfile router for adding file a local file to the IPFS network without any local node | |
app.get('/addfile', function(req, res) { | |
ipfs.files.add(testBuffer, function (err, file) { | |
if (err) { | |
console.log(err); | |
} | |
console.log(file) | |
}) | |
}) | |
//Getting the uploaded file via hash code. | |
app.get('/getfile', function(req, res) { | |
//This hash is returned hash of addFile router. | |
const validCID = 'HASH_CODE' | |
ipfs.files.get(validCID, function (err, files) { | |
files.forEach((file) => { | |
console.log(file.path) | |
console.log(file.content.toString('utf8')) | |
}) | |
}) | |
}) | |
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