• Node.js Video Tutorials

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 伺服器顯示以下表單:

Nexe Utility

上面的 Node.js 應用程式還註冊了靜態中介軟體,因此它顯示了放置在 static/images 資料夾中的影像。

Tutorialspoint Logo

現在,我們希望為這個 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
廣告

© . All rights reserved.