本文已在我的公众号Fabric技术分享原创首发。
转载请标明出处:
https://blog.csdn.net/qq_27818541/article/details/78599888
本文出自:【BigManing的博客】
前言
查询peer加入的channel名称。需要如下参数:
- peer节点
路由
app.js
//获取加入channel名称
app.get('/channels', function(req, res) {
logger.debug('================ GET CHANNELS ======================');
logger.debug('peer: ' + req.query.peer);
var peer = req.query.peer;
if (!peer) {
res.json(getErrorMessage('\'peer\''));
return;
}
query.getChannels(peer, req.username, req.orgname)
.then(function(
message) {
res.send(message);
});
});
具体实现
query.js
var getChannels = function(peer, username, org) {
// peer地址
var target = buildTarget(peer, org);
// channel实例
var channel = helper.getChannelForOrg(org);
//client实例
var client = helper.getClientForOrg(org);
// 获取当前注册用户
return helper.getRegisteredUsers(username, org).then((member) => {
//通过client调用api
return client.queryChannels(target);
}, (err) => {
logger.info('Failed to get submitter "' + username + '"');
return 'Failed to get submitter "' + username + '". Error: ' + err.stack ?
err.stack : err;
}).then((response) => {
//处理响应
if (response) {
logger.debug('<<< channels >>>');
var channelNames = [];
for (let i = 0; i < response.channels.length; i++) {
channelNames.push('channel id: ' + response.channels[i].channel_id);
}
logger.debug(channelNames);
return response;
} else {
logger.error('response_payloads is null');
return 'response_payloads is null';
}
}, (err) => {
logger.error('Failed to send query due to error: ' + err.stack ? err.stack :
err);
return 'Failed to send query due to error: ' + err.stack ? err.stack : err;
}).catch((err) => {
logger.error('Failed to query with error:' + err.stack ? err.stack : err);
return 'Failed to query with error:' + err.stack ? err.stack : err;
});
};
基本流程
API访问
echo "GET query Channels"
echo
curl -s -X GET \
"http://localhost:4000/channels?peer=peer1" \
-H "authorization: Bearer $ORG1_TOKEN" \
-H "content-type: application/json"
echo
echo
控制台打印:
GET query Channels
{"channels":[{"channel_id":"mychannel"}]}
后台打印:
[2017-10-16 11:07:43.315] [DEBUG] SampleWebApp - ================ GET CHANNELS ======================
[2017-10-16 11:07:43.316] [DEBUG] SampleWebApp - peer: peer1
[2017-10-16 11:07:43.316] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:43.317] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:43.319] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:43.319] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:43.320] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.320] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.320] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.322] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.323] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.324] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.325] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.325] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.326] [INFO] Helper - Successfully loaded member from persistence
[2017-10-16 11:07:43.334] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature: Signature {
r: <BN: e8f972a0b424c772ab360fdf4f0e3341a1d82398886cd7e93fcff87354f35f37>,
s: <BN: 3ecb06de15192b6a0797e0719d2db80128b9bd1491d472b61ecb4fb3893c55c6>,
recoveryParam: 0 }
[2017-10-16 11:07:43.346] [DEBUG] Query - <<< channels >>>
[2017-10-16 11:07:43.346] [DEBUG] Query - [ 'channel id: mychannel' ]