-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
109 lines (83 loc) · 2.44 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const Block = require("./block");
const Blockchain = require("./blockchain");
const Transaction = require("./transaction");
const BlockchainNode = require("./BlockchainNode");
const fetch = require("node-fetch")
const express = require("express");
//const bodyParser = require("body-parser");
let port = 3000;
//access the arguments
process.argv.forEach(function(val, index, array) {
port = array[2];
});
if (port === undefined) {
port = 3000;
}
const app = express();
let transactions = [];
let nodes = [];
let genesisBlock = new Block();
let blockchain = new Blockchain(genesisBlock);
const middlewares = [
//layout(),
// express.static(path.join(__dirname, 'public')),
express.urlencoded(),
express.json()
];
app.use(middlewares);
app.get("/resolve", function(req, res) {
nodes.forEach(function(node){
fetch('http://localhost:3000/blockchain')
.then(function(response){
return response.json()
})
.then(function(blockchain){
console.log(blockchain)
}).catch(err => {
console.log('Error' + err);
});
})
});
app.post("/nodes/register", function(req, res) {
let nodesLists = req.body.urls;
nodesLists.forEach(function(nodeDictionary) {
let node = new BlockchainNode(nodeDictionary["url"]);
nodes.push(node);
});
res.json(nodes);
});
app.get("/nodes", function(req, res) {
res.json(nodes);
});
app.get("/", function(req, res) {
res.send("Hello blockchain server");
});
app.get("/mine", function(req, res) {
let block = blockchain.getNextBlock(transactions);
blockchain.addBlock(block);
transactions = []
res.json(block);
});
app.post("/transactions", function(req, res) {
let to = req.body.to;
let from = req.body.from;
let amount = req.body.amount;
let transaction = new Transaction(from, to, amount);
transactions.push(transaction);
res.json(transactions);
});
app.get("/blockchain", function(req, res) {
res.json(blockchain);
// const transaction = new Transaction('Mary','Jerry',100)
// const genesisBlock = new Block();
// const blockchain = new Blockchain(genesisBlock)
// let block = blockchain.getNextBlock([transaction])
// blockchain.addBlock(block)
// let anotherTransaction = new Transaction("Azam","jerry",10)
// let block1 = blockchain.getNextBlock([anotherTransaction,transaction])
// blockchain.addBlock(block1)
// res.json(blockchain)
});
app.listen(port, function() {
console.log("Server has started on port :" + port);
});