- 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 專案並在任何其他環境中部署它變得很困難,因此需要對其進行打包,以便可以輕鬆地移植到其他機器。NPM 儲存庫中提供了許多打包工具。本章討論 nexe 打包工具,並概述其他一些打包庫。
Nexe
為了演示 Nexe 實用程式的工作原理,我們將使用以下指令碼建立一個 ExpressJs 應用程式:
Index.js
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname,"index.html"));
})
app.get('/process_get', function (req, res) {
// Prepare output in JSON format
response = {
first_name:req.query.first_name,
last_name:req.query.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
app.post("/process_post", )
var server = app.listen(5000, function () {
console.log("Express App running at http://127.0.0.1:5000/");
})
“/” 路由從以下指令碼呈現 HTML 表單:
Index.html
<html>
<body>
<form action = "/process_POST" method = "POST">
First Name: <input type = "text" name = "first_name"> <br>
Last Name: <input type = "text" name = "last_name"> <br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
上述 Node.js 伺服器顯示以下表單:
上面的 Node.js 應用程式還註冊了靜態中介軟體,因此它顯示了放置在 static/images 資料夾中的影像。
現在,我們希望為這個 Node.js 應用程式構建一個自執行檔案,該檔案將程式碼、子目錄以及資源打包到一個檔案中。Nexe 是一種命令列實用程式,可將您的 Node.js 應用程式編譯成單個可執行檔案。
Nexe 的特性
自包含應用程式
能夠使用不同的 Node.js 執行時執行多個應用程式。
分發二進位制檔案,無需 Node/npm。
更快地啟動和部署。
靈活的構建管道
跨平臺構建
在 Windows 上安裝 Nexe
執行以下操作以全域性安裝 Nexe:
npm install nexe -g
您還需要 Netwide Assembler (NASM) 工具。從 www.nasm.us 下載並安裝。
nexe 實用程式需要系統上安裝 Python。確保 Python 版本為 3.11 到 3.7 之間。
假設您正在將 nexe 用於 64 位 Windows 作業系統,您還需要從 Visual Studio 2022 安裝“使用 C++ 的桌面開發”工作負載。可以從 aka.ms 安裝。
(對於其他作業系統平臺,請按照 github.com/nodejs 上的說明操作)
安裝完所有先決條件後,在 CMD 終端中執行以下命令:
D:\expressApp>nexe index.js --build windows-x64-20.9.0 –verbose
編譯可能需要一段時間,但最終它將在應用程式資料夾中建立 expressApp.exe。
D:\expressApp
│ expressApp.exe
│ index.html
│ index.js
│ package-lock.json
│ package.json
│ users.json
│
├───node_modules
│ │ .package-lock.json
│ ├───body-parser
│ │ │ HISTORY.md
│ │ │ index.js
│ │ │ LICENSE
│ │ │ package.json
│ │ │ README.md
│ │ │ SECURITY.md
│ │ │ . . .
│ │ │ . . .
│ │ │ . . .
│ ├───express
│ │ │ History.md
│ │ │ index.js
│ │ │ LICENSE
│ │ │ package.json
│ │ │ Readme.md
│ │ │ . . .
│ │ │ . . .
│ │ │ . . .
└───public
└───images
logo.png
從命令列執行它,Node.js 伺服器將啟動。
D:\expressApp>expressapp Express App running at http://127.0.0.1:5000/
pkg 工具
pkg 工具是一個命令列介面,使開發人員能夠從 Node.JS 專案建立可執行檔案;允許您即使在未安裝 Node.JS 的環境中執行應用程式。
要安裝 pkg,請使用以下命令:
npm install -g pkg
然後使用 pkg 構建可執行檔案
pkg myapp.js
執行上述命令將生成三個程式;即 Windows、macOS 和 Linux 的可執行檔案。更多詳細資訊可以在 www.npmjs.com 上找到。
JXCore
JXcore 是一個開源專案,它為將原始檔和其他資源打包和加密到 JX 包中引入了獨特的功能。
根據您的作業系統和機器架構,從 https://github.com/jxcore 下載並安裝 JXcore 包。
要打包上述專案,您只需進入此目錄併發出以下 jx 命令。假設 index.js 是 Node.js 專案的入口檔案:
jx package index.js index
上述命令將打包所有內容,並建立以下兩個檔案:
index.jxp - 這是一箇中間檔案,包含編譯專案所需完整的專案詳細資訊。
index.jx - 這是一個包含完整包的二進位制檔案,已準備好交付給您的客戶或生產環境。
假設您的原始 Node.js 專案正在按以下方式執行:
node index.js command_line_arguments
使用 JXcore 編譯您的包後,可以按以下方式啟動它:
jx index.jx command_line_arguments
