This section describes how to run a sample standalone Node.js application which interacts with a Hyperledger Fabric block chain.
-
If you haven't already done so, see Setting Up The Application Development Environment to get your environment set up. The remaining steps assume that you are running inside the vagrant environment.
-
Issue the following commands to build the Node.js Client SDK:
cd /opt/gopath/src/github.com/hyperledger/fabric make node-sdk
-
Start the membership services and peer processes. We run the peer in dev mode for simplicity.
cd /opt/gopath/src/github.com/hyperledger/fabric/build/bin membersrvc > membersrvc.log 2>&1& peer node start --peer-chaincodedev > peer.log 2>&1&
-
Build and run chaincode example 2:
cd /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 go build CORE_CHAINCODE_ID_NAME=mycc CORE_PEER_ADDRESS=0.0.0.0:30303 ./chaincode_example02 > log 2>&1&
-
Put the following sample app in a file named app.js in the /tmp directory. Take a moment (now or later) to read the comments and code to begin to learn the Node.js Client SDK APIs.
/* * A simple application utilizing the Node.js Client SDK to: * 1) Enroll a user * 2) User deploys chaincode * 3) User queries chaincode */ // "HFC" stands for "Hyperledger Fabric Client" // we are using the path to the HFC because we have not globally installed the package var hfc = require('/opt/gopath/src/github.com/hyperledger/fabric/sdk/node'); var chain, user, chaincodeID; // Create a chain object used to interact with the chain. // You can name it anything you want as it is only used by client. chain = hfc.newChain("mychain"); // Initialize the place to store sensitive private key information chain.setKeyValStore( hfc.newFileKeyValStore('/tmp/keyValStore') ); // Set the URL to membership services and to the peer chain.setMemberServicesUrl("grpc://localhost:7054"); chain.addPeer("grpc://localhost:7051"); // The following is required when the peer is started in dev mode // (i.e. with the '--peer-chaincodedev' option) chain.setDevMode(true); // Begin by enrolling the user enroll(); // Enroll a user. function enroll() { console.log("enrolling user admin ..."); // Enroll "admin" which is preregistered in the membersrvc.yaml chain.enroll("admin", "Xurw3yU9zI0l", function(err, admin) { if (err) { console.log("ERROR: failed to register admin: %s",err); process.exit(1); } user = admin; deploy(); }); } // Deploy chaincode function deploy() { console.log("deploying chaincode; please wait ..."); // Construct the deploy request var req = { chaincodeName: 'mycc', fcn: "init", args: ["a", "100", "b", "200"] }; // Issue the deploy request and listen for events var tx = user.deploy(req); tx.on('complete', function(results) { // Deploy request completed successfully console.log("deploy complete; results: %j",results); // Set the testChaincodeID for subsequent tests chaincodeID = results.chaincodeID; query(); }); tx.on('error', function(err) { console.log("Failed to deploy chaincode: request=%j, error=%k",req,err); process.exit(1); }); } // Query chaincode function query() { console.log("querying chaincode ..."); // Construct a query request var req = { chaincodeID: chaincodeID, fcn: "query", args: ["a"] }; // Issue the query request and listen for events var tx = user.query(req); tx.on('complete', function (results) { console.log("query completed successfully; results=%j",results); process.exit(0); }); tx.on('error', function (err) { console.log("Failed to query chaincode: request=%j, error=%k",req,err); process.exit(1); }); }
-
Run the application as follows:
cd /tmp node app
-
Congratulations! You've successfully run your first Hyperledger Fabric application.
Next, see Hyperledger Fabric Client (HFC) SDK for Node.js to learn more about writing your own applications.