
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL 使用 Where 條件
- Node.js - MySQL 使用 Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果數量
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - 事件
當 JavaScript 用於 HTML 指令碼內部時,它通常處理使用者生成的事件,例如按鈕按下或滑鼠點選。Node.js 的核心 API 是一個非同步事件驅動的架構。但是,與客戶端 JavaScript 不同,它處理伺服器上的事件,例如檔案 IO 操作、伺服器的請求和響應等。
net.Server 物件在每次對等方連線到它時都會發出一個事件。
引用檔案的 ReadStream 在檔案開啟時以及每當有資料可供讀取時都會發出一個事件。
Node.js 識別多種型別的事件。每個事件都可以附加一個回撥函式。每當發生事件時,附加到它的回撥就會被觸發。Node.js 執行時始終在偵聽可能發生的事件。當發生任何它可以識別的事件時,就會執行其附加的回撥函式。
Node.js API 包括 events 模組,主要包含 EventEmitter 類。EventEmitter 物件觸發(或發出)某種型別的事件。您可以為某種型別的事件分配一個或多個回撥(偵聽器)。每當該事件觸發時,所有註冊的回撥都會按其註冊順序依次觸發。

以下是 Node.js API 中事件處理涉及的步驟。
首先,匯入 events 模組,並宣告一個 EventEmitter 類的物件
// Import events module var events = require('events'); // Create an eventEmitter object var eventEmitter = new events.EventEmitter();
使用以下語法將事件處理程式繫結到事件:
// Bind event and event handler as follows eventEmitter.on('eventName', eventHandler);
以程式設計方式觸發事件:
// Fire an event eventEmitter.emit('eventName');
示例
下面是一個簡單的示例,它將兩個偵聽器繫結到 EventEmitter 類的 on 事件
// Import events module var events = require('events'); // Create an eventEmitter object var eventEmitter = new events.EventEmitter(); // Create an event handler as follows var connectHandler = function connected() { console.log('connection successful.'); } // Bind the connection event with the handler eventEmitter.on('connection', connectHandler); // Bind the data_received event with the anonymous function eventEmitter.on('data_received', function(){ console.log('data received successfully.'); }); // Fire the connection event eventEmitter.emit('connection'); // Fire the data_received event eventEmitter.emit('data_received'); console.log("Program Ended.");
輸出
connection successful. data received successfully. Program Ended.
Node 應用程式中的任何非同步函式都將回調作為最後一個引數接受。回撥函式將錯誤作為第一個引數接受。
建立一個名為 input.txt 的文字檔案,內容如下。
Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!
建立一個名為 main.js 的 js 檔案,包含以下程式碼:
var fs = require("fs"); fs.readFile('input.txt', function (err, data) { if (err) { console.log(err.stack); return; } console.log(data.toString()); }); console.log("Program Ended");
這裡 fs.readFile() 是一個非同步函式,用於讀取檔案。如果在讀取操作期間發生錯誤,則 err 物件將包含相應的錯誤,否則 data 將包含檔案的內容。readFile 在讀取操作完成後將 err 和 data 傳遞給回撥函式,最終列印內容。
Program Ended Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!