使用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,否則追究其法律責任

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