使用Fabcar的js版本sdk测试

在第二章第5节里( 2.5 通知智能合约与网络互动 ),我们使用了peer节点的命令行方式与网络进行了互动。

在本节里,我们将需要SDK与网络互动,直接使用编写的代码与网络互动,为后面实现各种业务功能打基础。

在Fabirc的开发SDK里,有java,nodejs,golang,python这四种语言的版本供大家选用,这次我们选用nodejs版本,对于一个web开发者来说它是最简单最方便入门的,当然如果你精通java或其它两种语言,那另当别说。

为了方便入门,这次直接使用Fabric2.0 提供fabcar的测试用例进行操作说明,本节先把它运行起来,后面再详细解读。

先进入fabcar目录

cd $GOPATH/src/github.com/hyperledger/fabric-samples/fabcar/javascript

安装node依赖

npm install 

这样它就会根据根目录下的package.json的设置安装相关模块

如果出现以下错误

gyp ERR! find VS
gyp ERR! find VS msvs_version not set from command line or npm config
gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer
gyp ERR! find VS looking for Visual Studio 2015
gyp ERR! find VS - not found
gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
gyp ERR! find VS
gyp ERR! find VS **************************************************************
gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the "Desktop development with C++" workload.
gyp ERR! find VS For more information consult the documentation at:
gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
gyp ERR! find VS **************************************************************

则需要先安装Visual Studio和node-gyp

先以管理员的身份打开CMD,然后执行命令安装Visual Studio

npm install -g node-gyp
npm install --global --production windows-build-tools

最后可能会提示这个提醒,这个不需要管它

npm WARN [email protected] No repository field.

使用js与网络互动

先注册一个管理员账号

node enrollAdmin.js

然后注册一个用户账号

node registerUser.js

现在就可以使用查询功能

node query.js

接着我们更新区块链的数据看看

node invoke.js

再使用查询功能看看数据是否已经更新了

node query.js

至此我们就完成了一个通过js-sdk来与区块链互动的基本操作。

本文由小韦云原创,转载请注明出处:https://www.bctos.cn/doc/4/1830,否则追究其法律责任

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章