Hyperledger Fabric: Extending the FabCar network with ExpressJS REST Api

Kruk Matias
Coinmonks
Published in
3 min readJul 1, 2018

--

Before starting

I think a best way to expand the concepts learned with the basic examples provided by Hyperledger Fabric documentation, like the fabcar, is adding an extra layer of complexity. My intention in the next posts is the integration to a custom development in Salesforce.

In this case I’ll show to add an ExpressJS REST API to the fabcar network.

In my cases I did it in a Digital Ocean instance. But you can follow the steps with any instance (Heroku,…) with Ubuntu 16.04.

First of all, I recommend it the lecture of the related docs:

Understanding the Fabcar Network.

Introduction

Building your First Network

Then follow the writing your First App tutorial. I’d recommend it, doing it locally.

Fabcar Network Architecture:

Based on the documentation:

Fabcar uses the “basic-network” sample as its limited development network. It consists of a single peer node configured to use CouchDB as the state database, a single “solo” ordering node, a certificate authority (CA) and a CLI container for executing commands.

These components are bootstrapped by the ./startFabric.sh script, which also:

1. Creates a channel and joins the peer to the channel.

2. Installs the fabcar smart contract (fabric-samples/chaincode/fabcar) onto the peer’s file system and instantiates it on the channel (instantiate starts a container)

3. Calls the initLedger function to populate the channel ledger with 10 unique cars.

Installing ExpressJS

The next step is to go to your instance (Take in mind you need docker installed), and install the requirements.

An optional step is check everything is fine so far, doing again the Writing your First App, but now in the instance.

So if everything is correct, the next is the installation of ExpressJS in the folder fabric-samples/fabcar. Just follow the standard procedure as it explained here:

Then, it needs to create the server.js. You can check my example in my repository. The idea is to publish the query method described in the tutorial.

The NodeJS will run in this case at 8080, but you can change it.

So, now you can run simply:

node server.js

To check it out you can simply doing a curl from a terminal in your local instance:

curl http://localhost:8081/api/query

You should see something as this:

{“response”:”[{\”Key\”:\”CAR0\”, \”Record\”:{\”colour\”:\”blue\”,\”make\”:\”Toyota\”,\”model\”:\”Prius\”,\”owner\”:\”Tomoko\”}},{\”Key\”:\”CAR1\”, \”Record\”:{\”colour\”:\”red\”,\”make\”:\”Ford\”,\”model\”:\”Mustang\”,\”owner\”:\”Brad\”}},{\”Key\”:\”CAR2\”, \”Record\”:{\”colour\”:\”green\”,\”make\”:\”Hyundai\”,\”model\”:\”Tucson\”,\”owner\”:\”Jin Soo\”}},{\”Key\”:\”CAR3\”, \”Record\”:{\”colour\”:\”yellow\”,\”make\”:\”Volkswagen\”,\”model\”:\”Passat\”,\”owner\”:\”Max\”}},{\”Key\”:\”CAR4\”, \”Record\”:{\”colour\”:\”black\”,\”make\”:\”Tesla\”,\”model\”:\”S\”,\”owner\”:\”Adriana\”}},{\”Key\”:\”CAR5\”, \”Record\”:{\”colour\”:\”purple\”,\”make\”:\”Peugeot\”,\”model\”:\”205\”,\”owner\”:\”Michel\”}},{\”Key\”:\”CAR6\”, \”Record\”:{\”colour\”:\”white\”,\”make\”:\”Chery\”,\”model\”:\”S22L\”,\”owner\”:\”Aarav\”}},{\”Key\”:\”CAR7\”, \”Record\”:{\”colour\”:\”violet\”,\”make\”:\”Fiat\”,\”model\”:\”Punto\”,\”owner\”:\”Pari\”}},{\”Key\”:\”CAR8\”, \”Record\”:{\”colour\”:\”indigo\”,\”make\”:\”Tata\”,\”model\”:\”Nano\”,\”owner\”:\”Valeria\”}},{\”Key\”:\”CAR9\”, \”Record\”:{\”colour\”:\”brown\”,\”make\”:\”Holden\”,\”model\”:\”Barina\”,\”owner\”:\”Shotaro\”}}]”}

Now, we need to setup for accepting the external requests from the instance. The next are some guidelines needed in case of Digital Ocean.

Requirements for external access

The next applies at special for Digital Ocean instances, but you may find some similarities in the rest services as Heroku, Google, … . You need:

Configure NodeJS access from Nginx.

Configure iptables for web access from outside

Conclusions

This is a simple example for integration between Hyperledger Fabric and giving output through a simple ExpressJS Rest API app.

The next time will be an ambitous step: Integration of this API with a custom development in Salesforce.

Happy coding!

--

--