搭建RESTful API来使用Fabric Node SDK 开篇

Posted 术业有专攻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建RESTful API来使用Fabric Node SDK 开篇相关的知识,希望对你有一定的参考价值。

在Balance-Transfer中,有关于Node SDK比较完备的例子。

SDK的官方文档在这里:https://fabric-sdk-node.github.io/

Balance-Transfer中的各项配置项都已经准备了,如果需要重新生成artifacts,务必在生成之后相应的修改docker-compose.yaml,network-config.yaml

启动网络

运行./runApp.sh后,得到类似的结果:

Stopping peer1.org1.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping ca_peerOrg1            ... done
Stopping ca_peerOrg2            ... done
Stopping orderer.example.com    ... done
Removing peer1.org1.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing peer0.org2.example.com ... done
Removing ca_peerOrg1            ... done
Removing ca_peerOrg2            ... done
Removing orderer.example.com    ... done
Removing network artifacts_default

========== No containers available for deletion ==========


========== No images available for deletion ===========

Creating network "artifacts_default" with the default driver
Creating ca_peerOrg2         ... done
Creating orderer.example.com ... done
Creating ca_peerOrg1         ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done


============== node modules installed already =============

[2018-05-16 15:38:59.455] [INFO] SampleWebApp - ****************** SERVER STARTED ************************
[2018-05-16 15:38:59.461] [INFO] SampleWebApp - ***************  http://localhost:4000  ******************

在新的窗口运行 docker ps,得到

CONTAINER ID        IMAGE                        COMMAND                  CREATED              STATUS              PORTS                                            NAMES
5a59ea698c3f        hyperledger/fabric-peer      "peer node start"        About a minute ago   Up About a minute   0.0.0.0:8056->7051/tcp, 0.0.0.0:8058->7053/tcp   peer1.org2.example.com
a08384cc48cc        hyperledger/fabric-peer      "peer node start"        About a minute ago   Up About a minute   0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
027c68057998        hyperledger/fabric-peer      "peer node start"        About a minute ago   Up About a minute   0.0.0.0:7056->7051/tcp, 0.0.0.0:7058->7053/tcp   peer1.org1.example.com
6e738124e03b        hyperledger/fabric-peer      "peer node start"        About a minute ago   Up About a minute   0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp   peer0.org2.example.com
bfd5f1b1fe75        hyperledger/fabric-ca        "sh -c ‘fabric-ca-..."   About a minute ago   Up About a minute   0.0.0.0:7054->7054/tcp                           ca_peerOrg1
b8d0b10740b3        hyperledger/fabric-orderer   "orderer"                About a minute ago   Up About a minute   0.0.0.0:7050->7050/tcp                           orderer.example.com
9e807c581f33        hyperledger/fabric-ca        "sh -c ‘fabric-ca-..."   About a minute ago   Up About a minute   0.0.0.0:8054->7054/tcp                           ca_peerOrg2

执行测试

运行./testAPIs.sh ,得到如下结果:

POST request Enroll on Org1  ...

{"success":true,"secret":"","message":"Jim enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY0OTI1MzIsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1MjY0NTY1MzJ9.fnA1TvBIXD6TIVCJ_OfBgIdD42ljuYlv9zSAu_Hqv8M"}

ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY0OTI1MzIsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1MjY0NTY1MzJ9.fnA1TvBIXD6TIVCJ_OfBgIdD42ljuYlv9zSAu_Hqv8M

POST request Enroll on Org2 ...

{"success":true,"secret":"","message":"Barry enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY0OTI1MzQsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoiT3JnMiIsImlhdCI6MTUyNjQ1NjUzNH0.p3A_bcJGUltUjBI9u8J187aU2W2mQTgFsEalQNJcf-I"}

ORG2 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY0OTI1MzQsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoiT3JnMiIsImlhdCI6MTUyNjQ1NjUzNH0.p3A_bcJGUltUjBI9u8J187aU2W2mQTgFsEalQNJcf-I


POST request Create channel  ...

{"success":true,"message":"Channel ‘mychannel‘ created Successfully"}

POST request Join channel on Org1

{"success":true,"message":"Successfully joined peers in organization Org1 to the channel:mychannel"}

POST request Join channel on Org2

{"success":true,"message":"Successfully joined peers in organization Org2 to the channel:mychannel"}

POST Install chaincode on Org1

{"success":true,"message":"Successfully install chaincode"}

POST Install chaincode on Org2

{"success":true,"message":"Successfully install chaincode"}

POST instantiate chaincode on peer1 of Org1

主要测试了,注册用户,创建channel,Join channle,安装chaincode,初始化chaincode,调用chaincode,查询chaincode

以上是关于搭建RESTful API来使用Fabric Node SDK 开篇的主要内容,如果未能解决你的问题,请参考以下文章

No module named 'fabric.api'

用LoopBack搭建Node.js的 RESTful API环境

在Ubuntu上快速搭建基于Beego的RESTful API

如何保护 RESTful API

使用flask_restful+redis快速搭建简单API服务

基于hyperledger fabric 联盟链 + vue cli的项目搭建完整教程