{"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

<\/figure>\n<\/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

app.on(‘acuibot\u5236\u4f5cexe<\/a>tivate’, () => {<\/p>\n

if (mainWindow === null) {<\/p>\n

createWindow();<\/p>\n

}<\/p>\n

});<\/p>\n

“`<\/p>\n

\u4fee\u6539 `package.json` \u6587\u4ef6\uff0c\u5c06 Electron \u6dfb\u52a0\u4e3a\u542f\u52a8\u811a\u672c\uff1a<\/p>\n

“`json<\/p>\n

“scripts”: {<\/p>\n

“start”: “electron .”<\/p>\n

}<\/p>\n

“`<\/p>\n

\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n

“`<\/p>\n

npm start<\/p>\n

“`<\/p>\n

**3. \u5c06 Electron.js \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08EXE\uff09**<\/p>\n

\u5b89\u88c5 electron-packager\uff1a<\/p>\n

“`<\/p>\n

npm install electron-packager -g<\/p>\n

“`<\/p>\n

\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5305\u5e94\u7528\u7a0b\u5e8f\uff08\u8bf7\u6839\u636e\u9700\u8981\u66ff\u6362 \u201cmy-app-name\u201d\uff09\uff1a<\/p>\n

“`<\/p>\n

electron-packager . my-app-name –platform=win32 –arch=xvb6\u7a0b\u5e8f\u751f\u6210exe\u6587\u4ef6<\/a>64<\/p>\n

“`<\/p>\n

\u8be5\u547d\u4ee4\u5c06\u4e3a\u60a8\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u53ca\u6240\u6709\u4f9d\u8d56\u9879\u3002\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u5c06\u6b64\u6587\u4ef6\u5939\u4ea4\u4ed8\u7ed9\u5176\u4ed6\u4eba\uff0c\u4ed6\u4eec\u65e0\u9700\u5b89\u88c5 Node.js \u6216\u5176\u4ed6\u4f9d\u8d56\u9879\u5373\u53ef\u8fd0\u884c\u5b83\u3002<\/p>\n

\u672c\u6559\u7a0b\u5411\u60a8\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Node.js \u548c Electron.js \u5c06 HTML \u6587\u4ef6\u6253\u5305\u6210 EXE \u6587\u4ef6\u3002\u867d\u7136 Electron.js \u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f46\u5b83\u4e5f\u9002\u7528\u4e8e\u5c06\u57fa\u4e8e Web \u7684\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u7b80\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u548c\u53d1\u5e03\uff0c\u5e76\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5728\u5404\u79cd\u7cfb\u7edf\u548c\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u7684\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"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<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[8791,8790,24,116,12],"topic":[],"class_list":["post-4609","post","type-post","status-publish","format-standard","hentry","category-zhuomianruanjian","tag-pythonexe","tag-pyexe","tag-24","tag-116","tag-12"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/4609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=4609"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/4609\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=4609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=4609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=4609"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=4609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}