-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (53 loc) · 1.69 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
import express from "express";
import { getUsers, getBlockchain } from "./util/economy-blockchain.js";
const app = express();
const port = 27015;
app.get("/", (req, res) => {
res.send("Valid paths: /blockchain/TXID, /id/ID");
})
app.get("/blockchain", (req, res) => {
let block = getBlockchain();
//let count = Object.keys(block.transactions).length;
//let id = req.params.txid;
res.json(block.transactions);
});
app.get("/blockchain/latest", (req, res) => {
let block = getBlockchain();
let count = Object.keys(block.transactions).length;
if (req.params) {
return res.json(block.transactions[count-1]);
}
res.send("Could not retrieve TXID " + id);
});
app.get("/blockchain/:txid", (req, res) => {
let block = getBlockchain();
let count = Object.keys(block.transactions).length;
let id = req.params.txid;
if (req.params) {
for (let i = 0; i < count; i++) {
if (block.transactions[i].txid == id) {
return res.json(block.transactions[i]);
}
}
res.send("Could not retrieve TXID " + id)
}
});
app.get("/id/:id", (req, res) => {
let users = getUsers();
let count = Object.keys(users.accounts).length;
let id = req.params.id;
if (req.params) {
for (let i = 0; i < count; i++) {
if (users.accounts[i].userid == id) {
return res.json(users.accounts[i]);
}
}
res.send("Could not retrieve USERID " + id)
}
else {
res.send("Invalid ID");
}
});
app.listen(port, () => {
console.log(`App is listening on port ${port}!`)
})