Last active
December 9, 2019 17:06
-
-
Save PauloLuan/7ceebab16f0c7db2e71983a64bd9c640 to your computer and use it in GitHub Desktop.
NodeJS IPFS API example
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
const express = require('express'); | |
const ipfsClient = require('ipfs-http-client'); | |
const ipfs = ipfsClient('http://localhost:5001'); | |
const app = express(); | |
app.use(express.json()); | |
app.get('/', (req, res) => { | |
return res.send('Welcome to my IPFS app'); | |
}); | |
app.post('/upload', async (req, res) => { | |
const data = req.body; | |
console.log(data); | |
const fileHash = await addFile(data); | |
return res.send(`https://gateway.ipfs.io/ipfs/${ fileHash }`); | |
}); | |
const addFile = async ({ path, content }) => { | |
const file = { path: path, content: Buffer.from(content) }; | |
const filesAdded = await ipfs.add(file); | |
return filesAdded[0].hash; | |
} | |
app.listen(3000, () => { | |
console.log('Server running on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment