-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (34 loc) · 1.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express')
const app = express()
peerID = 'XTCFIL231021IPFS-4566A8975F1135XB468'; // Secret-Key (Expires in 3 Days)
// Don't move peerID to .env
const fs =require('fs');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var ipfsAPI = require('ipfs-api')
var ipfs = ipfsAPI('ipfs.infura.io', '5001', {protocol: 'https'})
// Serve static files
app.get('/', function (req, res) {
// res.send('GGWP')
res.sendFile(__dirname+'/public/index.html');
})
// Magic happens here
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// DEBUG:
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
console.log(req.file);
var data = new Buffer(fs.readFileSync(req.file.path));
ipfs.add(data, function (err,file){ // Upto 200MB
if(err){
console.log(err);
}
console.log(file);
res.send(file[0].hash); // 46 characters returned
})
})
app.get('/download/:ID',function(req,res){
console.log(req.params.ID);
res.redirect('https://ipfs.io/ipfs/'+req.params.ID);
})
app.listen(3000)