Node.js SDK Demo
This demo provides a Node.js chaincode and a program that uses the Hyperledger Fabric SDK for Node.js (fabric-nodejs-sdk) to invoke the chaincode to describe how to use a Node.js SDK to access BCS. For details about the APIs of Hyperledger Fabric SDK for Node.js, visit https://hyperledger.github.io/fabric-sdk-node/release-1.4/index.html.
This is a demo only and is not for actual use.
Preparations
Step |
Action |
Description |
---|---|---|
1 |
Install a development tool. |
Download and install the Node.js source code: https://nodejs.org/en/download/ |
2 |
Download the demo project code. |
Project code: nodejs-demo.zip |
Creating a BCS Instance
- Log in to the BCS console.
- Click Create BCS Instance in the upper right corner.
- Configure basic information about the BCS instance by referring to Table 1.
To ensure that the demo runs properly, set the parameters as described in the following table.
Table 1 Basic settings Parameter
Setting
Region
Retain the default value.
Enterprise project
Select default.
Instance Name
Enter node-sdk-demo.
Edition
Professional
Blockchain Type
Select Private.
Enhanced Hyperledger Fabric Version
v2.2
Consensus Mechanism
Select SOLO.
Resource Access Initial Password
Enter a password.
Confirm Password
Confirm the password.
- Click Next: Configure Resources. Table 2 describes the resource parameters.
Table 2 Resource configurations Parameter
Example
Environment Resources
Select Custom.
Cluster
Select Create a new CCE cluster.
AZ
Select an AZ.
ECS Specifications
Select the flavor for 4 vCPUs | 8 GB.
ECS Quantity
Enter 1.
High Availability
Select No.
VPC
Select Automatically create VPC.
Subnet
Select Automatically create subnet.
ECS Login Method
Select Password.
Password of Root User
If you do not enter a password here, the previously specified resource access initial password will be used.
Confirm Password
-
Use EIP of a CCE Node
Select Yes.
EIP Billed By
Retain the default value.
EIP Bandwidth
Set it to 5 Mbit/s.
- Click Next: Configure Blockchain. Table 3 describes the blockchain parameters.
Table 3 Blockchain configurations Parameter
Example
Blockchain Configuration
Select Custom.
Blockchain Mgmt. Initial Password
If you do not enter a password here, the previously specified resource access initial password will be used.
Confirm Password
-
Volume Type
Select SFS Turbo.
Storage Capacity of Peer Organization (GB)
Retain the default value.
Ledger Storage
Select File database (GoLevelDB).
Peer Organization
A peer organization named organization has been automatically created. Change the peer quantity to 1.
Channel Configuration
The organization organization has been added to the channel automatically. Retain this default setting.
Orderer Quantity
Retain the default value.
Security Mechanism
Select ECDSA.
NOTICE:Only ECDSA can be selected.
Configure Block Generation
Select No.
Enable Support for RESTful API
Select No.
- Click Next: Confirm.
- Confirm the configurations and finish the creation process.
Wait for several minutes. After a message is displayed indicating successful installation, check the status of the instance. If it is Normal, the deployment is completed.
Installing and Instantiating a Chaincode
- Log in to the BCS console.
- In the navigation pane on the left, click Instance Management.
- Find the instance you just created and click Manage Blockchain to go to the Blockchain Management console.
- On the login page, enter the username and password, and click Log In.
The username is admin, and the password is the Blockchain Mgmt. Initial Password set when you created the BCS instance. If you have not set this password, use the resource access initial password.
- Click in the upper left corner of the page.
The parameters for chaincode installation are as follows.
Parameter
Setting
Chaincode Name
Enter bcsysq.
Chaincode Version
Enter 1.0
Ledger Storage
File database (goleveldb)
Select All Peers
Check the box.
Organization & Peer
Select peer-0.
Language
Select Node.js.
Chaincode File
Add the downloaded chaincode file nodejs-chaincode.zip.
Chaincode Description
Enter a description of the chaincode.
- Click Install.
- After installing the chaincode, click Instantiate in the Operation column of the chaincode list.
The parameters for chaincode instantiation are as follows.
Parameter
Setting
Chaincode Name
Enter bcsysq.
Channel
Select channel.
Chaincode Version
Enter 1.0
Initialization Function
Enter init.
Chaincode Parameters
Enter a,200,b,250.
Endorsement Policy
Select Endorsement from any of the following organizations.
Endorsing Organizations
Select organization.
Privacy Protection Configuration
Select No.
Deploying the Application
- Download and decompress the demo project code package nodejs-demo.zip to the local PC, and use an IDE to open it.
This demo is compiled using Node.js. It contains the fabric-client library to enable money transfer from user A to user B by running the chaincode. Use the IDE that you prefer to open the script. The following table lists the files included in the project.
Table 4 Project content Fie
Description
invoke.js
Invoking the chaincode to transfer money from user A to user B.
query.js
Querying the chaincode to determine the account balance of user A.
sdk-config.js
Parsing the blockchain network configuration file sdk-config.json downloaded from the BCS console.
sdk-config.json
Containing the blockchain network configuration data.
- Download SDK configurations and certificates.
- On the Instance Management page, click Download Client Configuration on an instance card.
- Select SDK Configuration File and set the parameters as described in the following table.
Parameter
Description
Chaincode Name
Enter bcsysq.
NOTICE:The chaincode name must be the same as the name specified during chaincode installation and instantiation.
Certificate Root Path
Specify a path for storing certificates. In this example, the path is E:\code\temp.
Channel
Select channel.
Organization & Peer
Retain the default value.
Select Orderer Certificate.
Select Peer Certificates, select organization for Peer Organization, and select Administrator certificate.
- Click Download to download the SDK configuration file and the administrator certificates for the node-sdk-demo-orderer and organization organizations.
- Copy and decompress the package.
- Download the project source code javasdkdemo_src.zip and decompress it.
- Decompress the .zip package obtained in 2 and copy the content in the orderer and peer folders to the certificate storage path. Copy the sdk-config.json file to the directory where the certificate is stored and name the file node-sdk-demo-sdk-config.json.
Debugging the Application
- Open the sdk-config.js file, and change the path of the SDK configuration file to the path of node-sdk-demo-sdk-config.json.
- Run the node query.js command in the path where the project is located to query the account balance of user A. (As shown in the following figure, user A's balance is 10,000 in this example.)
- Run the node invoke.js command in the path where the project is located to execute the chaincode to transfer money from user A to user B. As shown in the following figure, the chaincode is successfully executed.
- Query the account balance of user A again. The result shows that the money has been successfully transferred. (As shown in the following figure, user A's balance is 9990 in this example.)
Description
- This demo uses demos in the Fabric community as references. For more demos, visit https://github.com/hyperledger/fabric-samples.
- Method of customizing an npm repository in the chaincode
- Create a personal configuration file named .npmrc in the path of the chaincode package. The content of the file is as follows: Registry=https://registry.npm.taobao.org/
- In this way, when the chaincode container is instantiated, the class library is extracted from the specified repository.
- Run the npm config get registry command to check whether the address of the customized npm repository has been correctly configured.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot