javascript - 在 Azure Function 上使用 web3.js 调用以太坊

标签 javascript node.js azure ethereum web3js

我正在尝试使用 Azure Function 使用 Web3.js 调用以太坊,代码似乎在命令 shell 中运行良好,但当我将其作为函数运行时出现错误。

在 Azure 门户上创建一个新的 Javascript Azure 函数:

  1. 转到平台功能 --> 开发工具 --> 高级工具 (kudu) --> 调试控制台 --> CMD

    光盘站点 cd wwwroot 光盘 npm install web3@^0.20.0

  2. 创建一个新的 code.js 文件

    const Web3 = require('web3'); var web3 = new Web3(); const httpProv = new Web3.providers.HttpProvider("http://:8545"); web3.setProvider(httpProv); console.log(web3.eth.blockNumber);

  3. 在 CMD shell 中执行此文件

    Node 代码.js

  4. 工作正常,我可以看到 HTTP Post 请求

    请求:

    POST/HTTP/1.1 用户代理: Node -XMLHttpRequest 接受:/ 内容类型:application/json 主持人: : 内容长度:63 连接:关闭

    {"jsonrpc":"2.0", "id":1,"method":"eth_blockNumber","params":[]}

回应:

HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date:
Content-Length:
Connection:close

{"jsonrpc":"2.0", "id":1,"result":"bla"}

我在 wwwroot 目录中创建了一个 package.json 文件

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "web3": "^0.20.0"        
      }
    }
  }
}

然后跑

npm install

当我执行 Azure 函数时,出现此错误:

2017-10-30T08:31:44.291 Function started (Id=f28edf73-fa02-406d-868e-0f23cd41e6e0)
2017-10-30T08:31:44.338 Exception while executing function: Functions.AZURE_FUNCTION. mscorlib: Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24.
2017-10-30T08:31:44.385 Function completed (Failure, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0, Duration=94ms)
2017-10-30T08:31:44.385 Exception while executing function: Functions.AZURE_FUNCTION
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.385 Function completed (Failure, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0, Duration=94ms)
2017-10-30T08:31:44.385 Executed 'Functions.AZURE_FUNCTION' (Failed, Id=f28edf73-fa02-406d-868e-0f23cd41e6e0)
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.385 Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is 'f28edf73-fa02-406d-868e-0f23cd41e6e0'
2017-10-30T08:31:44.385 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\AZURE_FUNCTION\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\AZURE_FUNCTION\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44.400 {"id":"4e755d0e-a2cf-4c4a-a734-aa750555a948","requestId":"4fa42aca-ff49-487a-a543-8f82295a1e5c","statusCode":500,"errorCode":0,"message":"Exception while executing function: Functions.AZURE_FUNCTION -> Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.\n    at Object.InvalidConnection (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\errors.js:31:16)\n    at HttpProvider.send (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\httpprovider.js:84:18)\n    at RequestManager.send (D:\\home\\site\\wwwroot\\AZURE_FUNCTION\\node_modules\\web3\\lib\\web3\\requestmanager.js:58:32)\n    at Eth.get [as blockNumber] (D:\\home\\site\\wwwroot\\FUNCTION_NAME\\node_modules\\web3\\lib\\web3\\property.js:107:62)\n    at module.exports (D:\\home\\site\\wwwroot\\FUNCTION_NAME\\index.js:8:25)\n    at D:\\Program Files (x86)\\SiteExtensions\\Functions\\1.0.11296\\bin\\azurefunctions\\functions.js:99:24"}
2017-10-30T08:31:44.400 Error: CONNECTION ERROR: Couldn't connect to node http://IP_ADDR:8545.
    at Object.InvalidConnection (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\errors.js:31:16)
    at HttpProvider.send (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\httpprovider.js:84:18)
    at RequestManager.send (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\requestmanager.js:58:32)
    at Eth.get [as blockNumber] (D:\home\site\wwwroot\FUNCTION_NAME\node_modules\web3\lib\web3\property.js:107:62)
    at module.exports (D:\home\site\wwwroot\FUNCTION_NAME\index.js:8:25)
    at D:\Program Files (x86)\SiteExtensions\Functions\1.0.11296\bin\azurefunctions\functions.js:99:24
2017-10-30T08:31:44  PID[10332] Information Sending response: 200.0 OK
2017-10-30T08:31:44  PID[10332] Information Sending response: 200.0 OK

最佳答案

以下是我解决此问题的步骤。

  1. 根据应用服务计划(而不是消费计划)创建 Azure Function 应用
  2. 启用网络套接字
  3. 将运行时版本切换为 Beta

请注意,Websock 和运行时版本选项仅在应用服务计划中可用。

关于javascript - 在 Azure Function 上使用 web3.js 调用以太坊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47054564/

相关文章:

javascript - 在 mousedown 处理程序中更改可见性属性时,不会在 Safari 或 Firefox 中触发单击处理程序

javascript - 为什么jquery在attr ('src'中返回对象)

javascript - Node.js 命令行程序(使用 Commander Node-Module)在执行时打开 index.js

Azure移动应用程序身份验证外部重定向URL

Azure IoT 中心证书

javascript - TinyMCE 4.7.4 不适用于包含查询字符串参数的 URL

javascript - 如何使特定按钮保持事件状态? CSS,Javascript

node.js - 如何指定/强制在 package.json 中使用特定的 node.js 版本?

Node.js Kue - 暂停具有多个实例的应用程序中的工作人员

azure - 我应该使用一个还是多个 CloudBlobContainer 来存储图像?