Node.js – 进程警告事件
每当 Node.js 事件发出进程警告时,便会触发一个“警告”事件。进程警告类似于描述异常情况(正引起用户注意)的错误。
每当 Node.js 遇到任何可能导致性能不佳或错误的糟糕编码实践时,它都可以发出警告。
语法
Event : 'warning'
示例 1
创建一个名为“warning.js”的文件,并复制以下代码。创建文件后,使用命令“node warning.js”运行此代码,如下面的示例所示
// Event: warning Demo Example
// Importing the process module
const process = require('process');
// Intentionally emited warning
process.emitWarning('This might raise a Warning');
// Firing a warning event
process.on('warning', (warning) => {
console.warn("Warning: " + warning.name);
console.warn("Warning Message - " + warning.message);
});输出
(node:34720) Warning: This might raise a Warning Warning: Warning Warning Message - This might raise a Warning
示例 2
我们来看看另一个示例。
// Event: Warning Demo Example
// Importing the process module
const process = require('process');
// Intentionally emited warning
process.emitWarning('CPU Usage is more than 90%');
// Firing a warning event
process.on('warning', (warning) => {
console.warn("warning stacktrace - " + warning.stack)
});输出
(node:38330) Warning: CPU Usage is more than 90% warning stacktrace - Warning: CPU Usage is more than 90% at Object.<anonymous> (/home/cg/root/4591873/main.js:5:9) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:389:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:504:3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP