-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.js
65 lines (53 loc) · 2.36 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
60
61
62
63
64
65
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, () => {
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
truffle_connect.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// 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);
});