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