我需要帮助处理与通过节点sdk到客户端的超级账本结构网关连接有关的查询。我写了一些API,使用下面的代码连接到Fabric网络。
const ccpPath = path.resolve(__dirname, '..', 'config', 'connection.json');
const gateway = new Gateway();
await gateway.connect(ccpPath, {
wallet,
identity: Username,
discovery: {
enabled: true,
asLocalhost: false
}
});
现在,我必须使用上述代码来连接每次调用API的控制器,但是我想一次使用它,因此一旦与网络建立连接,我就不需要一次又一次地连接。有什么办法可以实现它?如果可能的话建议我,以便我可以在我的API中实现。请让我知道是否需要更多信息?
预先感谢
是。
连接函数中的入口,并将网关声明为该函数的外部。
let gateway;
function connect() {
const ccpPath = path.resolve(__dirname, '..', 'config', 'connection.json');
gateway = new Gateway();
await gateway.connect(ccpPath, {
wallet,
identity: Username,
discovery: {
enabled: true,
asLocalhost: false
}
});
}
接着从app.js或应用程序的起点,调用该函数,您已连接。您不需要每次都连接,只需连接一次。