-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
58 lines (53 loc) · 1.79 KB
/
routes.js
File metadata and controls
58 lines (53 loc) · 1.79 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
var Block = require('./block');
var request = require('superagent');
var app = require('./app');
module.exports = (app) => {
// Watch for incoming requests of method GET and POST
// to the route http://localhost:8545/
// declares result in order to hold res.body.result
let result;
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// this is the call api call to GET ethereum data from Infura ********************************
app.get("/api", (req, res) => {
request
.get('https://api.infura.io/v1/jsonrpc/mainnet/eth_getBlockByNumber?params=["latest",false]')
.then(res => {
result = res.body.result;
console.log(result);
}).catch((e) => console.log(e));
});
// this is the api call to POST the Header Data
app.post("/api/block", (req, res) => {
console.log(result);
// ETH block header data Schema setup
let myData = new Block(
{
parentHash: result.parentHash,
ommersHash: result.ommersHash,
beneficiary: result.beneficiary,
stateRoot: result.stateRoot,
transactionsRoot: result.transactionsRoot,
receiptsRoot: result.receiptsRoot,
logsBloom: result.logsBloom,
difficulty: result.difficulty,
number: result.number,
gasLimit: result.gasLimit,
gasUsed: result.gasUsed,
timestamp: result.timestamp,
extraData: result.extraData,
mixHash: result.mixHash,
nonce: result.nonce,
}
);
myData.save()
.then(item => {
res.send("Block Data saved to database!!");
console.log(req.body);
})
.catch(err => {
res.status(400).send("Unable to save to database :( sad face");
});
});
};