寻求从托管网站调用Hyperledger Playground生成的REST API的分步教程
Great question! You absolutely can leverage the REST API generated from Hyperledger Playground in a hosted web app (replacing MySQL) — here's a detailed, step-by-step guide for non-local access:
By default, Hyperledger Playground runs against a local, ephemeral Fabric instance. For remote access, you’ll need to deploy your business network to a persistent, remote Hyperledger Fabric cluster:
- You can use managed services like IBM Blockchain Platform, or set up your own remote Fabric nodes on cloud servers like AWS, GCP, or Azure.
- Export your business network from Playground as a
.bnafile (use the "Export" option in the Playground interface).
- Install the Hyperledger Composer CLI tools on your local machine (or a deployment server) if you haven’t already:
npm install -g composer-cli composer-rest-server - Create a connection profile that points to your remote Fabric cluster (this file defines peer, orderer, and certificate authority endpoints).
- Import an admin identity card for your remote Fabric, then install and start your
.bnabusiness network:composer network install -c admin@remote-fabric -a your-business-network.bna composer network start -c admin@remote-fabric -n your-business-network -V 0.0.1 -A admin -S adminpw - Import the business network card to interact with the deployed network:
composer card import -f admin@your-business-network.card
The REST API you use will be served by the Composer REST Server, hosted on a publicly accessible server:
- On your remote server, install the Composer REST Server:
npm install -g composer-rest-server - Start the server with configurations that enable remote access and cross-origin requests (critical for hosted web apps):
Let’s break down these flags:composer-rest-server -c admin@your-business-network -n never -w true -a true -t true-c: Specifies the business network card to use-n never: Disables TLS for testing (enable it in production with-t trueand provide certificates)-w true: Enables anonymous access (replace with OAuth2/JWT in production for security)-a true: Enables CORS (cross-origin resource sharing) so your hosted web app can call the API-t true: Enables TLS encryption for secure production traffic
If you’re deploying to production, don’t skip these steps:
- Replace anonymous access with OAuth2 or JWT authentication (use the
-o trueflag when starting the REST Server to enable OAuth, then connect to your identity provider like Auth0 or Keycloak). - Use valid TLS certificates (letsencrypt works for free) to encrypt traffic between your web app and the REST Server.
- Restrict CORS origins to only your hosted web app’s domain (instead of
-a true, use-o https://your-web-app.com).
Now you can replace MySQL calls with the Hyperledger REST API endpoints. Here’s a quick example using JavaScript’s fetch API:
// Example: Fetch all assets from the business network (replace with your asset type) async function fetchAssets() { try { const response = await fetch('https://your-rest-server-domain.com/api/YourAssetType', { method: 'GET', headers: { 'Authorization': 'Bearer YOUR_JWT_TOKEN', // Include this if using authentication 'Content-Type': 'application/json' } }); const assets = await response.json(); // Use these assets just like you would data from MySQL renderAssetList(assets); } catch (error) { console.error('Failed to fetch assets:', error); } } // Example: Create a new asset (replace with your asset structure) async function createAsset(newAssetData) { try { const response = await fetch('https://your-rest-server-domain.com/api/YourAssetType', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_JWT_TOKEN', 'Content-Type': 'application/json' }, body: JSON.stringify(newAssetData) }); const createdAsset = await response.json(); console.log('Asset created:', createdAsset); } catch (error) { console.error('Failed to create asset:', error); } }
- Map your app’s CRUD operations to the REST API’s endpoints:
- Read:
GET /api/AssetTypeorGET /api/AssetType/{id} - Create:
POST /api/AssetType - Update:
PUT /api/AssetType/{id} - Delete:
DELETE /api/AssetType/{id}
- Read:
- Use Postman or curl to test all REST API endpoints first, ensuring they work with your remote Fabric network.
- Test your web app’s integration thoroughly: create, read, update, and delete assets to confirm data persists correctly in the Fabric ledger.
内容的提问来源于stack exchange,提问作者Eese

