{"id":4609,"date":"2023-08-25T12:18:57","date_gmt":"2023-08-25T04:18:57","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=4609"},"modified":"2023-08-25T12:18:57","modified_gmt":"2023-08-25T04:18:57","slug":"html%e6%89%93%e5%8c%85exenode%e8%bf%87%e7%a8%8b%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/08\/25\/html%e6%89%93%e5%8c%85exenode%e8%bf%87%e7%a8%8b%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"html\u6253\u5305exenode\u8fc7\u7a0b\u4ecb\u7ecd"},"content":{"rendered":"
\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Node.js \u5c06\u4e00\u4e2a HTML \u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684 EXE \u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u5c06 web \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u7a0b\u5e8f\u4ee5\u4fbf\u5728\u6ca1\u6709\u5b89\u88c5\u6d4f\u89c8\u5668\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u5c06 HTML \u6587\u4ef6\u6253\u5305\u4e3a EXE\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u60a8\u7684\u5ba2\u6237\u7aef\u4e0a\u5b89\u88c5\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u800c\u65e0\u9700\u5728\u7528\u6237\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u8f6f\u4ef6\u3002<\/p>\n
**\u539f\u7406**<\/p>\n
\u4e3a\u4e86\u5c06 HTML \u6587\u4ef6\u6253\u5305\u6210 EXE\uff0c\u6211\u4eec\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n
1. \u4f7f\u7528 Node.js \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 web \u670d\u52a1\u5668\uff0c\u5b83\u5c06\u76d1\u542c\u4e00\u4e2a\u7279\u5b9a\u7684\u7aef\u53e3\u5e76\u63d0\u4f9b HTML \u6587\u4ef6\u53ca\u5176\u4f9d\u8d56\u9879\u3002<\/p>\n
2. \u4f7f\u7528 Electron.js \u5c06\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u5305\u88c5\u6210\u4e00\u4e2a\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n
3. \u5c06 Electron.js \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08EXE\uff09\u3002<\/p>\n
\u6211\u4eec\u5c06\u5206\u6b65\u8fdb\u884c\u3002<\/p>\n
**1. \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Node.js Web \u670d\u52a1\u5668**<\/p>\n
\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\u6765\u653e\u7f6e\u6240\u6709\u76f8\u5173\u6587\u4ef6\u3002<\/p>\n
\u5728\u65b0\u6587\u4ef6\u5939\u4e2d\u521b\u5efa `app.js` \u6587\u4ef6\uff0c\u7136\u540e\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n
“`javascript<\/p>\n
const http = require(‘http’);<\/p>\n
const fs = require(‘fs’);<\/p>\n
const path = require(‘path’);<\/p>\n
const server = http.createServer((req, res) => {<\/p>\n
const filePath = path.join(__dirname, ‘index.html’);<\/p>\n
fs.readFile(filePath, (err, content) => {<\/p>\n
if (err) {<\/p>\n
res.writeHead(500, { ‘Content-Type’: ‘text\/plain’ });<\/p>\n
res.write(‘Error loading HTML file.’);<\/p>\n
res.end();<\/p>\n
} else {<\/p>\n
res.writeHead(200, { ‘Content-Type’: ‘text\/html’ });<\/p>\n
res.write(content);<\/p>\n
res.end();<\/p>\n
}<\/p>\n
});<\/p>\n
});<\/p>\n
const port = process.env.PORT || 3000;<\/p>\n
server.listen(port, () => {<\/p>\n
console.log(`Server is running on port ${port}`);<\/p>\n
});<\/p>\n
“`<\/p>\n
\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a `index.html` \u6587\u4ef6\uff0c\u5e76\u5c06\u8981\u663e\u793a\u7684 HTML \u5185\u5bb9\u6dfb\u52a0\u5230\u6b64\u6587\u4ef6\u4e2d\u3002<\/p>\n
**2. \u4f7f\u7528 Electron.js \u5305\u88c5 Web \u5e94\u7528\u7a0b\u5e8f**<\/p>\n
\u5b89\u88c5 Electron.js\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n
“`<\/p>\n
npm init<\/p>\n
npm install electron –save-dev<\/p>\n
“`<\/p>\n
\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a `electron.js` \u7684\u65b0\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230\u6b64\u6587\u4ef6\u4e2d\uff1a<\/p>\n
“`javascript<\/p>\n
const { app, BrowserWindow } = require(‘electron’);<\/p>\n
const path = require(‘path’);<\/p>\n
const url = require(‘url’);<\/p>\n
const h<\/p>\n
ttp = require(‘.\/app’);<\/p>\n let mainWindow;<\/p>\n function createWindow() {<\/p>\n mainWindow = new BrowserWindow({<\/p>\n width: 800,<\/p>\n height: 600,<\/p>\n webPreferences: {<\/p>\n nodeIntegration: true,<\/p>\n },<\/p>\n });<\/p>\n const serverUrl = url.format({<\/p>\n protocol: ‘http’,<\/p>\n hostname: ‘localhost’,<\/p>\n port: 3000,<\/p>\n pathname: ‘index.html’,<\/p>\n });<\/p>\n mainWindow.loadURL(serverUrl);<\/p>\n mainWindow.on(‘closed’, () => {<\/p>\n mainWindow = null;<\/p>\n });<\/p>\n }<\/p>\n app.on(‘ready’, createWindow);<\/p>\n app.on(‘window-all-closed’, () => {<\/p>\n if (process.platform !== ‘darwin’) {<\/p>\n app.quit();<\/p>\n }<\/p>\n });<\/p>\n<\/figure>\n<\/p>\n