Hyperledger Cookbook
上QQ阅读APP看书,第一时间看更新

Writing Node.js client-side code

We still need to work on one more file before we complete the end-to-end development. The Node.js client-side code provides a user interface, lets the user see the blockchain result in the browser, and invokes a method from a web page. Our Node.js client-side code can be found in index.ejs under the views folder. We can open this file and start to add some code in it.

For the Ship function, we use the jQuery post method to call the Node.js server-side Ship function. This will invoke the blockchain Ship method in the assetmgr chaincode, as follows:

<script>
$(document).ready(function(){
$("#ship").click(function(){
$.post("http://52.15.203.98:3000/ship", function(data){
var parsedJson = $.parseJSON($.parseJSON(data));
console.log(parsedJson);
});
});
});
</script>

The query function is similar to the Ship code; we use the jQuery get method to call the Node.js server-side query function, which will invoke the blockchain query method in the assetmgr chaincode.

Once the results return, it populates the data to the related fields on the UI:

$("#chainCodeQuery").click(function(){
$.get("http://52.15.203.98:3000/query",
function(data){
var parsedJson = $.parseJSON($.parseJSON(data));
$("#assetType").val(parsedJson.assetType);

});
});

We have now completed our end-to-end code. It is time to start our node server and do some quick experiments.