DAPP(2)- web3与私链通讯

556 阅读1分钟

安装web3(web3js.readthedocs.io/en/v1.5.2/w…

npm i web3

新建文件index.js并写入

const Web3 = require("web3");
// 连接指定端口的以太坊应用
let web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
console.log(web3.version)
// 获取应用的账户信息
web3.eth.getAccounts().then(res => console.log(res))

在上一步 (juejin.cn/post/703298…) 中 通过geth创建了一个本地私链,并运行在http服务,端口为8545。

现在通过创建web3实例,实例连接的端口为8545。 运行node index.js即可访问到之前geth创建的私链账户信息。此时web3即可与区块链正常通讯。