How to build a dapp on a private Ethereum network: Part — 4

Amey
Coinmonks
8 min readMay 22, 2020

--

This is a series of 5 articles for developing a decentralized application (dapp) on a network of 2 private Ethereum nodes without using any third-party APIs or apps (such as Infura, MetaMask, etc.). This tutorial covers the essential elements of what I learn during my research project at CSIR-CEERI.

I have covered sections I-VI in Parts 1–3. If you feel lost, you can read through the previous 3 parts whose links are mentioned at the bottom of this article in the All series links section.

Recently released — “Part 5 : Section VIII-IX

Table of Contents

Here, you can find a list of the sections and subsections that I have divided this tutorial into :

I. Creating the project

II. Installing prerequisites

III. Configuring the network

IV. Designing the smart contract

V. Setting-up the Truffle project

VI. Launching the network

→ VII. Building the web app

VIII. Testing the dapp

IX. Modifying the dapp

If you want to skip to a particular section, you can scroll down to the All series links section at the end of this article.

The flavor of a dish is all about its taste and feel. A well-cooked dish is truly complemented by the way it is served. A little garnishing will impart a wholesome appeal to it. Nothing fancy though, just some fresh coriander and a spoonful of cream.

By now, you have launched a private Ethereum Network of at least 2 nodes and have deployed the helloworld.sol smart contract onto the network. It is possible to interact with this system via the command line terminal. However, to make the dapp user friendly, you need a GUI.

To this end, we will develop the front-end and link it with the back-end (section VII) with the help of this article.

VII. Building the web app

Here, the focus is not on the design of web pages but on how to integrate a GUI with the private Ethereum network. However, it does help if the project has a nice interface. This is how the web pages would look like :

It’s not pretty, but it gets the job done. Now hurry up, we got code to write!!

1. Front-end : Client Interface

This requires web3 and truffle-contract packages, which in turn need build-essential to be setup. Open a terminal in the TruffleDapp folder and run :

Once the prerequisites are installed, create a folder named client inside the TruffleDapp directory and follow the steps as given.

A. CREATING INDEX PAGE

Create an index.html file inside the TruffleDapp/client repository with the following content :

B. ADDING CSS

The HTML files only define a basic skeleton for the web pages. To make them appealing, create a main.css file inside the client repository and type :

This CSS template has been borrowed from a different project, so it is not been tailor-made to suit this project.

4. ADDING JS FUNCTIONALITY

The HTML components used above need to be linked with corresponding functions that execute within the web browser to perform the required functions.

Here, the local private ethereum blockchain is used as web3Provider and TruffleContract from the truffle-contract package is used to access smart contract methods. Inside the client repository, open app.js file in an editor and type :

2. Setting Up Middleware : Express.js

Express.js is a light-weight web application framework which helps organize a web application on the server side and enables management of all facets of a web app, from routes, to handling requests and web pages. The first step is to setup an environment.

  • To install Express.js, open a terminal in the TruffleDapp folder (T1) and run :
  • To define the environment variables, a dotenv package is installed by executing in the same terminal window :
  • Following this, create a .env file in TruffleDapp and type in :

Here, PORT is the port at which we run the Node.js server, MODE is the network variable mentioned in truffle-config.js and LOCAL_NODE is the URL of the local private blockchain.

  • Next, make a server repository inside TruffleDapp. Create a main.js file inside the server folder, open it in an editor and type in :
  • Add the following statement under "scripts" of package.json file :
  • To test whether the web pages load as expected, run $ npm run start in T1 and open the URLs http://localhost:3000/index in a web browser. However, note that the web pages will not be functional since there is no geth node running at LOCAL_NODE = "http://127.0.0.1:8081", as mentioned in the .env file.

Note : In app.js, packages and external libraries imported by require cannot be understood by a browser. Hence, webpack is used as a bundler for modules. Its main purpose is to bundle JavaScript files for usage in a browser.

  • First, install the necessary modules. Run in T1 :
  • The environment variable declared in .env cannot be accessed directly on client side. Thus, they are defined here in DefinePlugin. The node and externals configurations are used to fix webpack errors on build. Add a webpack.config.js file with the following webpack configurations :
  • Add the following in "scripts" section of package.json :
  • Run $ npm run webpack in T1 to check for any webpack build error. If there is no error, testing the dapp can be started by running $ npm run dev in T1 and then opening the URLs http://localhost:3000/index in a web browser.

So we have built a simple decentralized application complete with an interactive GUI component. We will end this tutorial series with an article on testing the dapp and a highly-useful section on how to modify this dapp.

P.S. — Clap 10 times if you liked the article! Comment below to let me know your thoughts or if you want to share some hacks.

I will be publishing more such interesting articles shortly. Stalk me on Twitter to stay tuned.

All series links

In case you want to skip ahead and jump onto a specific section, you can use the links below for reference. Refer to the Table of Contents below to match a section with its corresponding topic.

Part-1 : Section I-III

Part-2 : Section IV

Part-3 : Section V-VI

Part 4 : Section VII

Part 5 : Section VIII-IX

Get Best Software Deals Directly In Your Inbox

--

--

Amey
Coinmonks

A product afficianado and an endorser of decentralization | Everything about me is at ameys.in