发布于2026-07-27 阅读(0)
扫一扫,手机访问
Node.js在Linux上的错误处理策略

先说一个核心判断:错误处理不是锦上添花,而是Node.js应用的底线工程。尤其在Linux环境下,进程稳定性、资源回收、日志持久化,每一项都直接关系到服务能否扛住生产流量。下面从全局兜底到防御性编程,拆解一套完整的错误处理策略。
全局错误处理器是防止Node.js进程因未处理错误崩溃的最后防线。需要监听两个核心事件:
uncaughtException:捕获未被try-catch或.catch()处理的同步/异步异常——比如代码逻辑错误、未捕获的Promise拒绝。处理时除了记录详细错误日志(堆栈信息必须包含),还要根据业务决定是否重启进程,避免无限崩溃。unhandledRejection:捕获未被处理的Promise拒绝,比如Promise.reject()后面没接.catch()。这里建议记录拒绝原因(reason)和关联的Promise对象,能帮助定位异步代码中的问题。示例代码:process.on('uncaughtException', (error) => {
console.error('未捕获异常:', error.stack);
// 记录日志后,可选择优雅退出或重启(需配合进程管理工具)
process.exit(1); // 生产环境建议用pm2等工具重启
});
process.on('unhandledRejection', (reason, promise) => {
console.error('未处理Promise拒绝:', reason, 'Promise:', promise);
});
模块级错误处理需要同步与异步场景分开应对:
try-catch包裹可能抛出异常的代码,比如文件读取、数据库查询。try {
const data = fs.readFileSync('/path/to/file.txt', 'utf8');
} catch (error) {
console.error('文件读取失败:', error.message);
}fs.readFile('/path/to/file.txt', 'utf8', (err, data) => {
if (err) {
console.error('文件读取失败:', err.message);
return;
}
console.log(data);
});.catch()捕获拒绝,或者async/await配合try-catch——后者更易读。async function fetchData() {
try {
const response = await axios.get('https://api.example.com/data');
console.log(response.data);
} catch (error) {
console.error('API请求失败:', error.response?.status || error.message);
}
}日志是错误排查的核心依据,要遵循结构化、分级、轮转三个原则:
const winston = require('winston');
const logger = winston.createLogger({
level: 'error', // 生产环境仅记录error及以上级别
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json() // 结构化日志便于ELK分析
),
transports: [
new winston.transports.File({ filename: 'logs/error.log' }), // 错误日志单独存储
new winston.transports.Console({ format: winston.format.simple() }) // 控制台输出简化格式
]
});winston-daily-rotate-file插件(Winston扩展)或Linux系统工具logrotate实现。示例(logrotate配置,/etc/logrotate.d/nodejs):/path/to/app.log {
daily
rotate 7
compress
missingok
notifempty
copytruncate
}监控是提前发现错误的关键,需要结合进程状态与错误指标:
--restart参数)。pm2 start app.js --name "my-app" --restart-delay=3000 # 崩溃后3秒重启const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'YOUR_DSN_HERE' }); // 配置Sentry项目地址通过设计降低错误对系统的影响:
cluster模块启动多个工作进程(共享端口),提高可用性。示例(集群模式):const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
cluster.fork(); // 启动子进程
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died, restarting...`);
cluster.fork(); // 子进程崩溃后重启
});
} else {
require('./app'); // 子进程执行应用逻辑
}从源头减少错误:
npm outdated),使用npm audit修复安全漏洞,避免依赖冲突。资源泄漏(比如文件描述符、数据库连接)会导致进程崩溃,需要正确释放:
fs.close()关闭打开的文件,或用stream.destroy()销毁流。pg-pool for PostgreSQL),并在应用退出时关闭连接。const { Pool } = require('pg');
const pool = new Pool({ connectionString: 'YOUR_DB_URL' });
// 使用连接
pool.query('SELECT * FROM users', (err, res) => {
if (err) throw err;
console.log(res.rows);
pool.end(); // 关闭连接池
});clearTimeout/clearInterval)和事件监听器(removeListener),避免内存泄漏。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8