Dapp搭建
##方式一 geth+js
1.centOs8安装geth客户端
https://its401.com/article/weixin_48288539/121287068#2geth_50
2.geth控制台一些命令
geth客户端启动
geth --datadir testchaindata --networkid 10001 console 2>1.txt
geth --testnet --fast --cache=512 --datadir "/data/ethereum/block_data" --rpc --rpcapi db,net,eth,web3,personal --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain "*" console
控制台查看节点数量
eth.blockNumber
创建账户
personal.newAccount("xxx")
查看账户数量
eth.accounts
启动挖矿
miner.start()
查看挖矿是否启动
eth.mining
##方式二 ganache+js
1.全局安装Ganache命令行模式(要求 Node.js >= v10.13.0 and npm >= 6.4.1.)
npm install ganache --global
这里遇到了一个问题是:npm dose not support Node.js v10.15.3
原来环境是node版本过低,解决方法删除原来nodejs,使用wget命令下载最新版本nodejs,这里是下载了nodejs17,问题解决。
启动ganache
ganache
2.安装web3js并测试
npm install web3
测试:
- 建立nodejs测试文件
:vim test.js
- 写入文件
var Web3 = require('web3'); var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); console.log(web3.version); web3.eth.getAccounts().then(console.log);
- 执行
node test.js
打印出web3版本,也证web3安装成功
3.express
express是基于 Node.js 平台,快速、开放、极简的 Web 开发框架
-
安装:
npm install express -g
npm install -g express-generator
-
创建工程
express -e MyDapp
cd MyDapp
启动工程
npm start
打卡浏览器http://127.0.0.1:3000查看效果,出现express Welcome to express即为成功。