Project Structure
Find repository on GitHub
Let's start by creating an application with NodeJS starter pack.
Once you have created a NodeJS application, create index.js for the homepage and upload.js for the file uploads in the routes folder. Meanwhile, create a folder named uploads under the routes folder to keep files.
Create a function in index.js for incoming GET requests to homepage.
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
npm install ipfs-api --save
Once you have installed the IPFS library, start writing upload.js.
Import ipfs-api to connect IPFS node and import fs, multer and path libraries to upload files to IPFS node.
const ipfsAPI = require('ipfs-api');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
var express = require('express');
var router = express.Router();
const MAX_SIZE = 52428800;
const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, path.join(__dirname, 'uploads'));
},
filename(req, file, cb) {
cb(null, `${Date.now()}.${file.mimetype.split('/')[1]}`);
},
});
const upload = multer({ storage });
const ipfsAPI = require('ipfs-api');
const ipfs = ipfsAPI({
host: '127.0.0.1',
port: 5001,
protocol: 'http'
});
/* upload GET endpoint. */
router.get('/', function(req, res, next) {
res.send('Upload endpoint!');
});
/* upload POST endpoint */
router.post('/', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(422).json({
error: 'File needs to be provided.',
});
}
const mime = req.file.mimetype;
if (mime.split('/')[0] !== 'image') {
fs.unlink(req.file.path);
return res.status(422).json({
error: 'File needs to be an image.',
});
}
const fileSize = req.file.size;
if (fileSize > MAX_SIZE) {
fs.unlink(req.file.path);
return res.status(422).json({
error: `Image needs to be smaller than ${MAX_SIZE} bytes.`,
});
}
const data = fs.readFileSync(req.file.path);
return ipfs.add(data, (err, files) => {
fs.unlink(req.file.path);
if (files) {
return res.json({
hash: files[0].hash,
});
}
return res.status(500).json({
error: err,
});
});
});
if (!req.file) {
return res.status(422).json({
error: 'File needs to be provided.',
});
}
const mime = req.file.mimetype;
if (mime.split('/')[0] !== 'image') {
fs.unlink(req.file.path);
return res.status(422).json({
error: 'File needs to be an image.',
});
}
const mime = req.file.mimetype;
if (mime.split('/')[0] !== 'image' || mime.split('/')[0] !== 'video') {
fs.unlink(req.file.path);
return res.status(422).json({
error: 'File needs to be an image or video.',
});
}
const fileSize = req.file.size;
if (fileSize > MAX_SIZE) {
fs.unlink(req.file.path);
return res.status(422).json({
error: `Image needs to be smaller than ${MAX_SIZE} bytes.`,
});
}
const data = fs.readFileSync(req.file.path);
return ipfs.add(data, (err, files) => {
fs.unlink(req.file.path);
if (files) {
return res.json({
hash: files[0].hash,
});
}
return res.status(500).json({
error: err,
});
});
With the HASH returned from the function, you can access your file via the IPFS gateway.
https://gateway.ipfs.io/ipfs/ {HASH}
https://gateway.ipfs.io/ipfs/QmaJVawuxkDnKxE6tFDT1V6xVjag3wDCG2BVBDjfwEwkLJ