-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
87 lines (74 loc) · 2.5 KB
/
index.mjs
File metadata and controls
87 lines (74 loc) · 2.5 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import express from 'express';
import ipfsAPI from 'ipfs-api'
import cors from 'cors'
import bodyParser from 'body-parser'
const app = express();
app.use(cors());
app.use(express.json());
const ipfs = ipfsAPI()
app.post('/save/', async function (req, res) {
const isValidSaveRequest = (req, res) => {
// Check the request body has at least a body.
console.log(req.body)
if (!req.body || !req.body['text']) {
res.status(400);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
error: {
id: 'no-body',
message: 'blob must have a body'
}
}));
return false;
}
return true;
};
console.log("valid request:" + isValidSaveRequest(req, res))
try {
const result = await ipfs.files.add(new Buffer(req.body['text']));
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(result));
} catch(err) {
res.status(500);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
error: {
id: 'unable-to-save-blob',
message: 'The blob was received but we were unable to save it to ipfs.'
}
}));
}
});
app.get('/get/', async function (req, res) {
const isValidSaveRequest = (req, res) => {
// Check the request body has at least a body.
if (!req.query) {
res.status(400);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
error: {
id: 'no-id',
message: 'request must have an ipfsPath'
}
}));
return false;
}
return true;
};
console.log("valid request:" + isValidSaveRequest(req, res))
try {
const result = await ipfs.files.cat(req.query['ipfsPath']);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ data: result.toString('utf8') }));
} catch(err) {
res.status(500);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
error: {
id: 'unable-to-read-ipfs',
message: 'The ipfsPath was received but we were unable to get it.'
}
}));
}
});
app.listen(5001, () => console.log('Digger up and listening on port 5001'))