22 / 26Part 6: Create the clientBeginner
Prerequisites
This tutorial takes you step by step from preparing your development environment to understanding the code.
First, confirm that your development environment is configured correctly.
Step 1: Install Node.js and set up the project
-
Install Node.js: Node.js runs JavaScript on the server side. If it isn't on your machine yet, get it from the Node.js website and install it.
-
Create a project directory: In your terminal, go to the location where your project should live and run these commands:
shell mkdir chromia-book-review-democd chromia-book-review-demo -
Initialize a Node.js project: Generate a
package.jsonfile with this command:shell npm init -y
Step 2: Install required packages
With the project in place, install the packages it depends on:
npm install typescript postchain-client readline @types/nodetypescript: adds TypeScript support.postchain-client: provides the tools you use to communicate with the Chromia blockchain.readline: a core Node.js module for reading user input from the command line.
Step 3: Configure TypeScript
TypeScript reads its settings from a file called tsconfig.json. Generate it by running:
npx tsc --initYou get a tsconfig.json file with default settings, which you can adjust to fit your project.
Step 4: Integrate the code
With the packages installed, you can add the code to your project:
-
In your project directory, create a TypeScript file called
book_review.ts. Leave it empty for now; you will fill it in over the following sections. -
Node.js can't run TypeScript directly, so transpile it to JavaScript first:
shell npx tscThe compiled JavaScript is written to a
book_review.jsfile. -
Run the result with Node.js:
shell node book_review.jsThis runs your code, which interacts with the Chromia blockchain according to what you have written.
Your development environment is now ready: the required packages are installed and TypeScript is integrated into the project. The next section walks through an example client.