-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (61 loc) · 2.22 KB
/
server.js
File metadata and controls
75 lines (61 loc) · 2.22 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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');
const dbConfig = require('./api/config/database.config.js');
const mongoose = require('mongoose');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
//db connection
mongoose.connect(dbConfig.url, {
useNewUrlParser: true
}).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
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){
// we wil get the list of all the accounts and send them 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);
});
});
require('./api/routes/user.routes.js')(app);
require('./api/routes/medicine.batch.routes.js')(app);
require('./api/routes/user.batch.interaction.routes.js')(app);
require('./api/routes/fake.medicine.route.js')(app);
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);
});