forked from arvindkalra/express-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (46 loc) · 1.63 KB
/
server.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
const express = require('express');
const app = express();
const port = 3000 || process.env.PORT;
const Web3 = require('web3');
const truffle_connect = require('./connection/app.js');
const bodyParser = require('body-parser');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use('/', express.static('public_static'));
app.get('/getAccounts', (req, res) => {
console.log("**** GET /getAccounts ****");
truffle_connect.start(function (answer) {
res.send(answer);
})
});
app.post('/getBalance', (req, res) => {
console.log("**** GET /getBalance ****");
console.log(req.body);
let currentAcount = req.body.account;
truffle_connect.refreshBalance(currentAcount, (answer) => {
let account_balance = answer;
truffle_connect.start(function(answer){
// get list of all accounts and send it along with the response
let all_accounts = answer;
response = [account_balance, all_accounts]
res.send(response);
});
});
});
app.post('/sendCoin', (req, res) => {
console.log("**** GET /sendCoin ****");
console.log(req.body);
let amount = req.body.amount;
let sender = req.body.sender;
let receiver = req.body.receiver;
truffle_connect.sendCoin(amount, sender, receiver, (balance) => {
res.send(balance);
});
});
app.listen(port, () => {
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
truffle_connect.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log("Express Listening at http://localhost:" + port);
});