Connect To Chain
- create
Node.js
project
$ cd Desktop/
$ mkdir contract
$ cd contract/
$ npm init
$ npm install [email protected] --save
$ vim index.js
index.js
const Web3 = require('web3');
const ethereumUri = 'http://localhost:8485';
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(ethereumUri));
if(!web3.isConnected()) {
throw new Error(`unable to connect to ethereum node at ${ethereumUri}`);
} else {
console.log(`connected to ehterum node at ${ethereumUri}`);
console.log(`coinbase: ${web3.eth.coinbase}`);
console.log(`balance: ${web3.fromWei(web3.eth.getBalance(web3.eth.coinbase), 'ether')} ETH`);
console.log(web3.eth.accounts);
}
/Users/yen/Desktop/contract
├── index.js
├── node_modules
└── package.json
$ node index.js
connected to ehterum node at http://localhost:8485
coinbase: 0x88dd6b191d4bca6b059dd35ac0cffb2ffd072d62
balance: 0.014662 ETH
[ '0x88dd6b191d4bca6b059dd35ac0cffb2ffd072d62' ]