{"id":10233,"date":"2023-08-25T12:18:57","date_gmt":"2023-08-25T04:18:57","guid":{"rendered":"https:\/\/www.yimenapp.cn\/yun\/?p=10233"},"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.yimenapp.cn\/yun\/2023\/10233\/","title":{"rendered":"html\u6253\u5305exenode\u8fc7\u7a0b\u4ecb\u7ecd"},"content":{"rendered":"<p>\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<p>**\u539f\u7406**<\/p>\n<p>\u4e3a\u4e86\u5c06 HTML \u6587\u4ef6\u6253\u5305\u6210 EXE\uff0c\u6211\u4eec\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>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<p>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<p>3. \u5c06 Electron.js \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08EXE\uff09\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u5206\u6b65\u8fdb\u884c\u3002<\/p>\n<p>**1. \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Node.js Web \u670d\u52a1\u5668**<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\u6765\u653e\u7f6e\u6240\u6709\u76f8\u5173\u6587\u4ef6\u3002<\/p>\n<p>\u5728\u65b0\u6587\u4ef6\u5939\u4e2d\u521b\u5efa `app.js` \u6587\u4ef6\uff0c\u7136\u540e\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>&#8220;`javascript<\/p>\n<p>const http = require(&#8216;http&#8217;);<\/p>\n<p>const fs = require(&#8216;fs&#8217;);<\/p>\n<p>const path = require(&#8216;path&#8217;);<\/p>\n<p>const server = http.createServer((req, res) =&gt; {<\/p>\n<p>  const filePath = path.join(__dirname, &#8216;index.html&#8217;);<\/p>\n<p>  fs.readFile(filePath, (err, content) =&gt; {<\/p>\n<p>    if (err) {<\/p>\n<p>      res.writeHead(500, { &#8216;Content-Type&#8217;: &#8216;text\/plain&#8217; });<\/p>\n<p>      res.write(&#8216;Error loading HTML file.&#8217;);<\/p>\n<p>      res.end();<\/p>\n<p>    } else {<\/p>\n<p>      res.writeHead(200, { &#8216;Content-Type&#8217;: &#8216;text\/html&#8217; });<\/p>\n<p>      res.write(content);<\/p>\n<p>      res.end();<\/p>\n<p>    }<\/p>\n<p>  });<\/p>\n<p>});<\/p>\n<p>const port = process.env.PORT || 3000;<\/p>\n<p>server.listen(port, () =&gt; {<\/p>\n<p>  console.log(`Server is running on port ${port}`);<\/p>\n<p>});<\/p>\n<p>&#8220;`<\/p>\n<p>\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<p>**2. \u4f7f\u7528 Electron.js \u5305\u88c5 Web \u5e94\u7528\u7a0b\u5e8f**<\/p>\n<p>\u5b89\u88c5 Electron.js\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>npm init<\/p>\n<p>npm install electron &#8211;save-dev<\/p>\n<p>&#8220;`<\/p>\n<p>\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<p>&#8220;`javascript<\/p>\n<p>const { app, BrowserWindow } = require(&#8216;electron&#8217;);<\/p>\n<p>const path = require(&#8216;path&#8217;);<\/p>\n<p>const url = require(&#8216;url&#8217;);<\/p>\n<p>const h<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/17\/14\/27820918565.jpg\" \/><\/figure>\n<\/p>\n<p>ttp = require(&#8216;.\/app&#8217;);<\/p>\n<p>let mainWindow;<\/p>\n<p>function createWindow() {<\/p>\n<p>  mainWindow = new BrowserWindow({<\/p>\n<p>    width: 800,<\/p>\n<p>    height: 600,<\/p>\n<p>    webPreferences: {<\/p>\n<p>      nodeIntegration: true,<\/p>\n<p>    },<\/p>\n<p>  });<\/p>\n<p>  const serverUrl = url.format({<\/p>\n<p>    protocol: &#8216;http&#8217;,<\/p>\n<p>    hostname: &#8216;localhost&#8217;,<\/p>\n<p>    port: 3000,<\/p>\n<p>    pathname: &#8216;index.html&#8217;,<\/p>\n<p>  });<\/p>\n<p>  mainWindow.loadURL(serverUrl);<\/p>\n<p>  mainWindow.on(&#8216;closed&#8217;, () =&gt; {<\/p>\n<p>    mainWindow = null;<\/p>\n<p>  });<\/p>\n<p>}<\/p>\n<p>app.on(&#8216;ready&#8217;, createWindow);<\/p>\n<p>app.on(&#8216;window-all-closed&#8217;, () =&gt; {<\/p>\n<p>  if (process.platform !== &#8216;darwin&#8217;) {<\/p>\n<p>    app.quit();<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p>app.on(&#8216;ac<a href=\"https:\/\/exe.yimenapp.com\/tool-3956.html\">uibot\u5236\u4f5cexe<\/a>tivate&#8217;, () =&gt; {<\/p>\n<p>  if (mainWindow === null) {<\/p>\n<p>    createWindow();<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p>&#8220;`<\/p>\n<p>\u4fee\u6539 `package.json` \u6587\u4ef6\uff0c\u5c06 Electron \u6dfb\u52a0\u4e3a\u542f\u52a8\u811a\u672c\uff1a<\/p>\n<p>&#8220;`json<\/p>\n<p>&#8220;scripts&#8221;: {<\/p>\n<p>  &#8220;start&#8221;: &#8220;electron .&#8221;<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>npm start<\/p>\n<p>&#8220;`<\/p>\n<p>**3. \u5c06 Electron.js \u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08EXE\uff09**<\/p>\n<p>\u5b89\u88c5 electron-packager\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>npm install electron-packager -g<\/p>\n<p>&#8220;`<\/p>\n<p>\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>&#8220;`<\/p>\n<p>electron-packager . my-app-name &#8211;platform=win32 &#8211;arch=x<a href=\"https:\/\/www.yimenexe.com\/ruanjian-kaifa-3474.html\">vb6\u7a0b\u5e8f\u751f\u6210exe\u6587\u4ef6<\/a>64<\/p>\n<p>&#8220;`<\/p>\n<p>\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<p>\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":"<p>\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":23,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[17323,17322,539,14,22],"topic":[],"class_list":["post-10233","post","type-post","status-publish","format-standard","hentry","category-diannaoruanjian","tag-pythonexe","tag-pyexe","tag-539","tag-14","tag-22"],"_links":{"self":[{"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/posts\/10233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/comments?post=10233"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/posts\/10233\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/media?parent=10233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/categories?post=10233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/tags?post=10233"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenapp.cn\/yun\/wp-json\/wp\/v2\/topic?post=10233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}