forked from gimmixorg/factory-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.17 KB
/
index.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
const IPFS = require('ipfs');
const polka = require('polka');
const fileUpload = require('express-fileupload');
const bodyparser = require('body-parser');
const cors = require('cors');
const path = require('path');
const repoPath = path.resolve('./.ipfs');
let node;
IPFS.create({ repo: repoPath }).then(_node => {
node = _node;
});
polka()
.use(cors(), fileUpload(), bodyparser.json())
.use((_req, res, next) => {
res.json = data => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
};
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PATCH, PUT, DELETE, OPTIONS'
);
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, Content-Type, X-Auth-Token'
);
next();
})
.post('/upload', async (req, res) => {
const data = await node.add(req.files.files.data);
return res.json({ hash: data.path });
})
.post('/uploadJSON', async (req, res) => {
const body = req.body;
const data = await node.add(JSON.stringify(body));
return res.json({ hash: data.path });
})
.listen(process.env.PORT || 3000);