
- 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 條件查詢
- Node.js - MySQL 排序
- 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 - 模組
Node.js 中的模組是獨立且可重用的程式碼集合,可以匯入到任何 Node.js 應用程式中。顧名思義,模組支援 Node.js 應用程式開發的模組化和結構化方法。與其將應用程式的所有函式、類和方法都放在單個 .js 檔案中,不如根據其相關性將這些資源排列在單獨的檔案(稱為模組)中。這樣可以更好地控制 Node.js 應用程式的維護和故障排除。
Node.js 執行在 V8 JavaScript 引擎上,該引擎解釋 JavaScript 程式碼。所有伺服器端程序都由應用程式透過 require() 函式匯入的相關 Node.js 模組處理。Node.js 模組是一個函式、類和其他可重用程式碼庫,儲存在一個或多個 .js 檔案中。
匯入 Node.js 模組的一個典型示例是如何啟動 Node.js 伺服器。啟動 Node.js 伺服器需要 http 模組中定義的 createServer() 函式。因此,在呼叫之前需要匯入 http 模組。
http = require('node:http'); listener = function (request, response) { ... ... }; server = http.createServer(listener); server.listen(3000);
模組型別
Node.js 中的每個模組都有其自己的上下文,並實現 CommonJS 模組標準。Node.js 中的模組可以是以下三種類型之一:
第三方模組
這些模組由獨立開發者開發,可在 NPM 倉庫中使用。如果要在應用程式中整合其功能,則應全域性或本地安裝模組到 Node.js 專案資料夾中。
Express 模組就是這樣一個模組的示例。要安裝,請使用以下任何命令:
npm install express -g //install globally or npm install express –save //install locally
然後,您可以在應用程式中匯入已安裝的模組。例如:
var express = require('express');
內建模組
Node.js 執行時軟體附帶 V8 JavaScript 引擎,捆綁了許多核心模組,這些模組執行重要的伺服器端任務,例如管理事件迴圈、執行檔案 IO 和特定於作業系統的函式等。內建或核心模組的示例包括 http、fs、console 等。這些模組是預安裝的,但必須使用 require() 函式匯入它們(除了少數模組,如 process、buffer 和 console 模組,它們是全域性物件)。例如:
var fs = require('fs');
本地模組
本地模組是一個 .js 檔案,其中儲存 Node.js 應用程式所需的一個或多個函式或類的定義。此類模組存在於相同的 Node.js 應用程式資料夾中,也應使用 require() 函式包含在應用程式中。
Node.js 應用程式中的每個 .js 檔案都有一個特殊的 module 物件。它的 exports 屬性將 .js 檔案中的某個函式、物件或變數暴露給外部程式碼。
示例
以下 functions.js 檔案包含三個函式,它們返回 x 的 y 次冪的值、x 的平方根和 x 的對數值。這些函式是為外部使用而匯出的。
exports.power = function powerval( x, y) { var val = Math.pow(x,y); return val; } exports.root = function rootval(x,y) { var val = Math.sqrt(x); return val; } exports.log = function logval(x) { var val = Math.log10(x); return val; }
現在,我們將在另一個 Node.js 應用程式 main.js 中使用這些匯出的函式。首先匯入本地 functions 模組,然後呼叫從中匯入的函式。
const mymodule = require('./mathfunctions'); console.log("10 Raised to 3:" + mymodule.power(10,3)); console.log("Square root of 10: "+ mymodule.root(10)); console.log("Log of 1000: " + mymodule.log(1000));
輸出
10 Raised to 3:1000 Square root of 10: 3.1622776601683795 Log of 1000: 3