Skip to content

Instantly share code, notes, and snippets.

@PauloLuan
Last active December 9, 2019 17:06
Show Gist options
  • Save PauloLuan/7ceebab16f0c7db2e71983a64bd9c640 to your computer and use it in GitHub Desktop.
Save PauloLuan/7ceebab16f0c7db2e71983a64bd9c640 to your computer and use it in GitHub Desktop.
NodeJS IPFS API example
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