Building the backend

Let's first build the backend of the app. First of all, run npm install inside the Initial directory to install the required dependencies for our backend. Before we get into coding the backend, make sure geth is running with rpc enabled. If you are running geth on a private network, then make sure mining is also enabled. Finally, make sure that account 0 exists and is unlocked. You can run geth on a private network with rpc and mining enabled and also unlocking account 0:

geth --dev --mine --rpc --unlock=0

One final thing you need to do before getting started with coding is deploy the ownership contract using the code we saw in the Chapter 4, Smart Contracts and copy the contract address.

Now let's create a single server, which will serve the HTML to the browser and also accept socket.io connections:

var express = require("express");   
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io")(server);
server.listen(8080);

Here, we are integrating both the express and socket.io servers into one server running on port 8080.

Now let's create the routes to serve the static files and also the home page of the app. Here is the code to do this:

app.use(express.static("public")); 
app.get("/", function(req, res){
res.sendFile(__dirname + "/public/html/index.html");
})

Here, we are using the express.static middleware to serve static files. We are asking it to find static files in the public directory.

Now let's connect to the geth node and also get a reference to the deployed contract so that we can send transactions and watch for events. Here is the code to do this:

var Web3 = require("web3"); 

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var proofContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"fileHash","type":"string"}],"name":"get","outputs":[{"name":"timestamp","type":"uint256"},{"name":"owner","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner","type":"string"},{"name":"fileHash","type":"string"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"status","type":"bool"},{"indexed":false,"name":"timestamp","type":"uint256"},{"indexed":false,"name":"owner","type":"string"},{"indexed":false,"name":"fileHash","type":"string"}],"name":"logFileAddedStatus","type":"event"}]);

var proof = proofContract.at("0xf7f02f65d5cd874d180c3575cb8813a9e7736066");

The code is self-explanatory. Just replace the contract address with the one you got.

Now let's create routes to broadcast transactions and get information about a file. Here is the code to do this:

app.get("/submit", function(req, res){ 
var fileHash = req.query.hash;
var owner = req.query.owner;
proof.set.sendTransaction(owner, fileHash, {
from: web3.eth.accounts[0],
}, function(error, transactionHash){
if (!error)
{
res.send(transactionHash);
}
else
{
res.send("Error");
}
})
})
app.get("/getInfo", function(req, res){
var fileHash = req.query.hash;
var details = proof.get.call(fileHash);
res.send(details);
})

Here, the /submit route is used to create and broadcast transactions. Once we get the transaction hash, we send it to the client. We are not doing anything to wait for the transaction to mine. The /getInfo route calls the get method of the contract on the node itself instead of creating a transaction. It simply sends back whatever response it got.

Now let's watch for the events from the contract and broadcast it to all the clients. Here is the code to do this:

proof.logFileAddedStatus().watch(function(error, result){ 
if(!error)
{
if(result.args.status == true)
{
io.send(result);
}
}
})

Here, we check whether the status is true, and if it's true, only then do we broadcast the event to all the connected socket.io clients.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.101