3080 字
15 分钟
NodeJS技术

NodeJS技术#

概述#

Node.js是Web3和区块链开发中不可或缺的技术栈。本章节详细介绍了Node.js在区块链开发中的应用,包括JavaScript基础、事件循环机制、异步编程等核心概念,以及区块链相关的Node.js开发实践。

1. JavaScript基础#

1.1 异常处理机制#

1.1.1 try-catch语句#

try {
throw new Error('1')
} catch(error) {
console.log('error')
}
// 输出结果:error

1.1.2 异步异常处理#

try {
setTimeout(function() {
console.log('b');
}, 0);
} catch (error) {
console.log('error');
}
console.log('out try catch')
// 输出结果:
// out try catch
// b

注意:setTimeout是异步操作,异常不会在try-catch中捕获。

1.1.3 Promise异常处理#

try {
new Promise(() => {
throw new Error('new promise throw error');
});
} catch (error) {
console.log('error');
}
// 输出结果:Error: new promise throw error

注意:Promise中的异常需要通过.catch()方法捕获。

1.2 事件循环机制#

1.2.1 基本概念#

事件循环机制用于管理异步API的回调函数什么时候回到主线程中执行。

  • Node.js采用异步IO模型
  • 同步API在主线程中执行
  • 异步API在底层的C++维护的线程中执行
  • 异步API的回调函数也会在主线程中执行

1.2.2 事件循环的六个阶段#

1. Timers阶段

  • 用于存储定时器的回调函数(setInterval, setTimeout)
  • 执行到期的定时器回调

2. Pending callbacks阶段

  • 执行与操作系统相关的回调函数
  • 比如启动服务器端应用时监听端口操作的回调函数

3. Idle, prepare阶段

  • 系统内部使用
  • 程序员通常不需要关心

4. Poll阶段

  • 存储I/O操作的回调函数队列
  • 比如文件读写操作的回调函数
  • 如果事件队列中有回调函数,则执行它们直到清空队列
  • 否则事件循环将在此阶段停留一段时间以等待新的回调函数进入

等待条件

  • 如果setImmediate队列(check阶段)中存在要执行的调函数,不会等待
  • 如果timers队列中存在要执行的回调函数,也不会等待

5. Check阶段

  • 存储setImmediate的回调函数
  • 执行setImmediate的回调

6. Closing callbacks阶段

  • 执行与关闭事件相关的回调
  • 例如关闭数据库连接的回调函数等

1.2.3 宏任务与微任务#

宏任务

  • setInterval
  • setTimeout
  • setImmediate
  • I/O操作

微任务

  • Promise.then
  • Promise.catch
  • Promise.finally
  • process.nextTick

1.2.4 执行顺序#

console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
Promise.resolve().then(() => {
console.log('Promise resolved');
});
console.log('End');
// 输出结果:
// Start
// End
// Promise resolved
// Timeout

执行规则

  1. 微任务优先级高于宏任务
  2. 当微任务事件队列中存在可以执行的回调函数时,事件循环在执行完当前阶段的回调函数后会暂停进入事件循环的下一个阶段
  3. 会立即进入微任务的事件队列中开始执行回调函数
  4. 当微任务队列中的回调函数执行完成后,事件循环才会进入到下一个阶段开始执行回调函数
  5. process.nextTick的优先级高于其他微任务

1.3 ES6新特性#

1.3.1 核心特性#

  • 类的支持:面向对象编程
  • 模块化:import/export语法
  • 箭头操作符:简化函数定义
  • let/const块作用域:变量作用域控制
  • 字符串模板:模板字符串语法
  • 解构:数组和对象解构
  • 参数默认值/不定参数/拓展参数:函数参数增强
  • for-of遍历:迭代器语法
  • generator:生成器函数
  • Map/Set:新的数据结构
  • Promise:异步编程解决方案

1.3.2 在区块链开发中的应用#

// 使用async/await处理异步操作
async function getBlockData(blockNumber) {
try {
const block = await web3.eth.getBlock(blockNumber);
return block;
} catch (error) {
console.error('获取区块数据失败:', error);
throw error;
}
}
// 使用解构获取交易信息
const { from, to, value, gas } = transaction;
// 使用Map存储合约地址映射
const contractAddresses = new Map();
contractAddresses.set('USDT', '0xdAC17F958D2ee523a2206206994597C13D831ec7');

1.4 类继承#

1.4.1 继承方法#

1. 原型链法

function Animal() {
this.name = 'animal';
}
Animal.prototype.sayName = function(){
alert(this.name);
};
function Person() {}
Person.prototype = Animal.prototype; // 人继承自动物
Person.prototype.constructor = 'Person'; // 更新构造函数为人

2. 属性复制法

function Animal() {
this.name = 'animal';
}
Animal.prototype.sayName = function() {
alert(this.name);
};
function Person() {}
for(prop in Animal.prototype) {
Person.prototype[prop] = Animal.prototype[prop];
} // 复制动物的所有属性到人
Person.prototype.constructor = 'Person'; // 更新构造函数为人

3. 构造器应用法

function Animal() {
this.name = 'animal';
}
Animal.prototype.sayName = function() {
alert(this.name);
};
function Person() {
Animal.call(this); // apply, call, bind方法都可以
}

1.4.2 多重继承#

多重继承通过属性复制法实现,将所有父类的prototype属性复制后,子类自然拥有类似行为和属性。

1.5 this指向#

在JavaScript中,this指向对象本身,但在不同情况下this的指向会发生变化:

// 全局作用域
console.log(this); // 指向全局对象(浏览器中是window,Node.js中是global)
// 对象方法
const obj = {
name: 'test',
getName: function() {
return this.name; // this指向obj
}
};
// 构造函数
function Person(name) {
this.name = name; // this指向新创建的实例
}
// 箭头函数
const obj = {
name: 'test',
getName: () => {
return this.name; // this指向外层作用域
}
};

1.6 apply、call和bind#

三者都可以把一个函数应用到其他对象上,但有一些区别:

function Person() {}
Person.prototype.sayName = function() {
alert(this.name);
}
var obj = {name: 'michaelqin'}; // 注意这是一个普通对象,它不是Person的实例
// 1) apply
Person.prototype.sayName.apply(obj, [param1, param2, param3]);
// 2) call
Person.prototype.sayName.call(obj, param1, param2, param3);
// 3) bind
var sn = Person.prototype.sayName.bind(obj);
sn([param1, param2, param3]); // bind需要先绑定,再执行
sn(param1, param2, param3); // bind需要先绑定,再执行

区别

  • apply:接受数组作为参数
  • call:接受逗号分隔的无限多个参数列表
  • bind:绑定后需要再次调用才能执行

1.7 caller、callee和arguments#

function parent(param1, param2, param3) {
child(param1, param2, param3);
}
function child() {
console.log(arguments); // { '0': 'mqin1', '1': 'mqin2', '2': 'mqin3' }
console.log(arguments.callee); // [Function: child]
console.log(child.caller); // [Function: parent]
}
parent('mqin1', 'mqin2', 'mqin3');
  • caller:返回调用当前函数的函数
  • callee:返回当前正在执行的函数
  • arguments:函数的所有参数列表,它是一个类数组的变量

1.8 Map和Set#

1.8.1 Map#

Map是一组键值对的结构,具有极快的查找速度:

const map = new Map();
map.set('name', 'Alice');
map.set('age', 25);
console.log(map.get('name')); // Alice

1.8.2 Set#

Set和Map类似,也是一组key的集合,但不存储value。由于key不能重复,所以Set中没有重复的key:

const set = new Set();
set.add(1);
set.add(2);
set.add(1); // 重复值,不会添加
console.log(set.size); // 2

1.8.3 WeakSet和WeakMap#

WeakSet

  • 成员都是对象
  • 成员都是弱引用,可以被垃圾回收机制回收
  • 可以用来保存DOM节点,不容易造成内存泄漏
  • 不能遍历,方法有add、delete、has

WeakMap

  • 只接受对象作为键名(null除外)
  • 键名是弱引用,键值可以是任意的
  • 键名所指向的对象可以被垃圾回收,此时键名是无效的
  • 不能遍历,方法有get、set、has、delete

2. Node.js架构#

2.1 整体架构#

Node.js主要分为三层:

应用app >> V8及node内置架构 >> 操作系统
  • V8:Node.js运行的环境,可以理解为Node虚拟机
  • Node内置架构:又可分为三层
    • 核心模块(JavaScript实现)
    • C++绑定
    • libuv + CAes + http

2.2 核心模块#

Node.js的核心模块包括:

  • fs:文件系统操作
  • http:HTTP服务器和客户端
  • path:路径处理
  • crypto:加密功能
  • util:工具函数
  • events:事件处理

2.3 在区块链开发中的应用#

2.3.1 文件系统操作#

const fs = require('fs');
const path = require('path');
// 读取合约ABI
const contractABI = JSON.parse(fs.readFileSync(path.join(__dirname, 'contracts', 'Token.json'), 'utf8'));
// 写入私钥文件
fs.writeFileSync('privateKey.txt', privateKey);

2.3.2 HTTP服务器#

const http = require('http');
const express = require('express');
const app = express();
// 创建API端点
app.get('/balance/:address', async (req, res) => {
try {
const balance = await web3.eth.getBalance(req.params.address);
res.json({ balance: balance.toString() });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('服务器运行在端口3000');
});

2.3.3 加密功能#

const crypto = require('crypto');
// 生成随机私钥
const privateKey = crypto.randomBytes(32);
// 计算哈希
const hash = crypto.createHash('sha256').update(data).digest('hex');
// 签名
const sign = crypto.createSign('sha256');
sign.update(data);
const signature = sign.sign(privateKey, 'hex');

3. 区块链开发实践#

3.1 Web3.js集成#

3.1.1 安装和配置#

Terminal window
npm install web3
const Web3 = require('web3');
// 连接到以太坊节点
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
// 或者连接到本地节点
const web3 = new Web3('http://localhost:8545');

3.1.2 基本操作#

// 获取账户余额
async function getBalance(address) {
const balance = await web3.eth.getBalance(address);
return web3.utils.fromWei(balance, 'ether');
}
// 发送交易
async function sendTransaction(from, to, value) {
const tx = {
from: from,
to: to,
value: web3.utils.toWei(value, 'ether'),
gas: 21000
};
const result = await web3.eth.sendTransaction(tx);
return result;
}
// 部署合约
async function deployContract(abi, bytecode, from) {
const contract = new web3.eth.Contract(abi);
const deploy = contract.deploy({
data: bytecode,
arguments: []
});
const result = await deploy.send({
from: from,
gas: 1500000
});
return result;
}

3.2 事件监听#

3.2.1 合约事件监听#

// 监听合约事件
const contract = new web3.eth.Contract(abi, contractAddress);
contract.events.Transfer({
filter: { from: '0x...' }, // 过滤条件
fromBlock: 'latest'
}, (error, event) => {
if (error) {
console.error('事件监听错误:', error);
return;
}
console.log('Transfer事件:', event);
});

3.2.2 区块监听#

// 监听新区块
web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
if (error) {
console.error('区块监听错误:', error);
return;
}
console.log('新区块:', blockHeader.number);
});

3.3 错误处理#

3.3.1 异步错误处理#

// 使用try-catch处理异步错误
async function handleAsyncOperation() {
try {
const result = await someAsyncOperation();
return result;
} catch (error) {
console.error('操作失败:', error);
throw error;
}
}
// 使用Promise.catch处理错误
someAsyncOperation()
.then(result => {
console.log('操作成功:', result);
})
.catch(error => {
console.error('操作失败:', error);
});

3.3.2 重试机制#

async function retryOperation(operation, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await operation();
} catch (error) {
if (i === maxRetries - 1) {
throw error;
}
console.log(`重试 ${i + 1}/${maxRetries}:`, error.message);
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
}
}
}

3.4 性能优化#

3.4.1 连接池#

const Web3 = require('web3');
class Web3Pool {
constructor(urls, maxConnections = 10) {
this.urls = urls;
this.connections = [];
this.maxConnections = maxConnections;
this.currentIndex = 0;
}
getConnection() {
if (this.connections.length < this.maxConnections) {
const url = this.urls[this.currentIndex % this.urls.length];
const web3 = new Web3(url);
this.connections.push(web3);
this.currentIndex++;
return web3;
}
return this.connections[this.currentIndex++ % this.connections.length];
}
}

3.4.2 缓存机制#

const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 600 }); // 10分钟过期
async function getCachedBalance(address) {
const cacheKey = `balance_${address}`;
let balance = cache.get(cacheKey);
if (!balance) {
balance = await web3.eth.getBalance(address);
cache.set(cacheKey, balance);
}
return balance;
}

4. 最佳实践#

4.1 代码组织#

4.1.1 模块化设计#

config/database.js
module.exports = {
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
database: process.env.DB_NAME || 'blockchain',
username: process.env.DB_USER || 'postgres',
password: process.env.DB_PASS || 'password'
};
// services/blockchain.js
class BlockchainService {
constructor(web3) {
this.web3 = web3;
}
async getBalance(address) {
return await this.web3.eth.getBalance(address);
}
async sendTransaction(tx) {
return await this.web3.eth.sendTransaction(tx);
}
}
module.exports = BlockchainService;

4.1.2 错误处理中间件#

function errorHandler(err, req, res, next) {
console.error('错误:', err);
if (err.code === 'INSUFFICIENT_FUNDS') {
return res.status(400).json({
error: '余额不足',
code: 'INSUFFICIENT_FUNDS'
});
}
if (err.code === 'NETWORK_ERROR') {
return res.status(503).json({
error: '网络错误',
code: 'NETWORK_ERROR'
});
}
res.status(500).json({
error: '服务器内部错误',
code: 'INTERNAL_ERROR'
});
}

4.2 安全考虑#

4.2.1 私钥管理#

const crypto = require('crypto');
class KeyManager {
constructor() {
this.encryptionKey = process.env.ENCRYPTION_KEY;
}
encryptPrivateKey(privateKey, password) {
const cipher = crypto.createCipher('aes-256-cbc', password);
let encrypted = cipher.update(privateKey, 'hex', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
decryptPrivateKey(encryptedKey, password) {
const decipher = crypto.createDecipher('aes-256-cbc', password);
let decrypted = decipher.update(encryptedKey, 'hex', 'hex');
decrypted += decipher.final('hex');
return decrypted;
}
}

4.2.2 输入验证#

const Joi = require('joi');
const transactionSchema = Joi.object({
from: Joi.string().pattern(/^0x[a-fA-F0-9]{40}$/).required(),
to: Joi.string().pattern(/^0x[a-fA-F0-9]{40}$/).required(),
value: Joi.string().pattern(/^\d+$/).required(),
gas: Joi.number().integer().min(21000).max(1000000).optional()
});
function validateTransaction(data) {
const { error, value } = transactionSchema.validate(data);
if (error) {
throw new Error(`验证失败: ${error.details[0].message}`);
}
return value;
}

4.3 监控和日志#

4.3.1 日志记录#

const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console()
]
});
// 使用日志
logger.info('交易发送成功', { txHash: '0x...', from: '0x...', to: '0x...' });
logger.error('交易失败', { error: error.message, tx: tx });

4.3.2 性能监控#

const performance = require('perf_hooks');
function measurePerformance(name, fn) {
return async (...args) => {
const start = performance.performanceNow();
try {
const result = await fn(...args);
const end = performance.performanceNow();
console.log(`${name} 执行时间: ${end - start}ms`);
return result;
} catch (error) {
const end = performance.performanceNow();
console.log(`${name} 执行失败,耗时: ${end - start}ms`);
throw error;
}
};
}
// 使用示例
const getBalanceWithTiming = measurePerformance('getBalance', getBalance);

5. 总结#

Node.js在Web3和区块链开发中发挥着重要作用。通过深入理解JavaScript基础、事件循环机制、异步编程等核心概念,结合Node.js的架构特点,我们可以构建高效、安全的区块链应用。

在实际开发中,需要注意:

  1. 异步编程:正确处理异步操作和错误
  2. 性能优化:使用连接池、缓存等机制提升性能
  3. 安全考虑:妥善管理私钥,验证输入数据
  4. 错误处理:建立完善的错误处理和重试机制
  5. 监控日志:记录关键操作,便于问题排查

通过不断学习和实践,我们可以更好地掌握Node.js在Web3开发中的应用,构建更优秀的区块链应用。


本文档基于playground-web3仓库中的NodeJS技术模块整理,结合Web3技术特点进行了扩展和补充。

NodeJS技术
https://website-truelovings-projects.vercel.app/posts/web3/03-nodejs技术/
作者
欢迎来到StarSky的网站!
发布于
2025-09-05
许可协议
CC BY-NC-SA 4.0