child_process.fork 没有在打包的电子应用程序内启动快速服务器 [英] child_process.fork not starting an express server inside of packaged electron app

查看:21
本文介绍了child_process.fork 没有在打包的电子应用程序内启动快速服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子应用程序,我不仅需要为用户运行界面,还需要启动一个快速服务器,为通过网络连接的人提供文件.

I have an electron app where I need not only to run the interface to the user but also start an express server that will serve files for people connected through the network.

如果我正常启动 electron 和 express 服务器,我一切正常,但我非常有信心我需要服务器在不同的线程中运行,以避免界面缓慢甚至服务器出现问题.

I have everything working if I start both electron and the express server normally, but I'm pretty confident that I will need the server running in a different thread to avoid slugish interface and even problems with the server.

就此而言,我尝试使用 child_process.fork 运行我的 express 服务器,当我使用 npm start 时它可以工作,但是当我使用 electron-builder 创建时一个 .exe,安装的程序不会启动 express 服务器.

For that matter I tried to run my express server using the child_process.fork and it worked when I use npm start, but when I use electron-builder to create an .exe, the installed program doesn't start the express server.

我尝试使用以下命令立即运行我的服务器:

I tried to run my server right away using:

require('child_process').fork('app/server/mainServer.js')

我尝试了几处更改,在文件前加上__dirnameprocess.resourcesPath,甚至硬编码生成的文件路径;更改 fork 选项以传递 cwd: __dirnamedetached: truestdio: 'ignore';甚至尝试将 spawnprocess.execPath 一起使用,它也可以与 npm start 一起使用,但在打包时不会(它会不断打开新实例我的应用程序,在你这样做之后似乎很明显呵呵)

I tried several changes, prefixing the file with __dirname, process.resourcesPath and even hard coding the generated file path; changing the fork options to pass cwd: __dirname, detached: true and stdio: 'ignore'; and even tried using spawn with process.execPath, which will also work with npm start but won't when packaged (it keeps opening new instances of my app, seems obvious after you do hehe)

注意:如果我不立即分叉并要求服务器脚本,则使用 require('server/mainServer.js') 它适用于打包的应用程序,所以最喜欢的问题是不是快递本身.

Note: If I don't fork and require the server script right away, using require('server/mainServer.js') it works on the packaged app, so the problem most like isn't the express itself.

注意2:我有asar: false来解决其他问题,所以这里不是问题解决者.

Note 2: I have asar: false to solve other problems, so this is not the problem solver here.

我建立了一个小 git 项目来展示我的问题:

I put up a small git project to show my problem:

https://github.com/victorivens05/electron-fork-error

我们将不胜感激.

推荐答案

在 Samuel Attard (https://github.com/MarshallOfSound)我能够解决问题(他实际上为我解决了)

With the great help from Samuel Attard (https://github.com/MarshallOfSound) I was able to solve the problem (he solved for me actually)

正如他所说:

the default electron app will launch the first file path provided to it
so `electron path/to/thing` will work
in a packaged state, that launch logic is not present
it will always run the app you have packaged regardless of the CLI args passed to it
you need to handle the argument manually yourself
and launch that JS file if it's passed in as the 1st argument
The first argument to fork simply calls `process.execPath` with the first
argument being the path provided afaik
The issue is that when packaged Electron apps don't automatically run the
path provided to them
they run the app that is packaged within them

换句话说.fork 实际上是 spawn 使用 process.execPath 执行并将 fork 的第一个参数作为 spawn 的第二个参数传递.

In other words. fork is actually spawn being executed with process.execPath and passing the fork's first argument as the second for spawn.

在打包的应用程序中发生的情况是 process.execPath 不是电子,而是打包的应用程序本身.因此,如果您尝试spawn,应用程序将一遍又一遍地打开.

What happens in a packaged app is that the process.execPath isn't electron but the packaged app itself. So if you try to spawn, the app will be open over and over again.

所以,Samuel 的建议是这样实现的:

So, what Samuel suggest was implemented like this:

if (process.argv[1] === '--start-server') {
   require('./server/mainServer.js')
   return
}

require('./local/mainLocal.js')
require('child_process').spawn(process.execPath, ['--start-server'])

这样,打包后的应用第一次执行时,process.argv[1]为空,所以服务器不会启动.然后它将执行电子部分(在我的例子中为 mainLocal)并重新启动应用程序,但这次传递 argv.下次应用启动时,它会启动服务器并停止执行,因此应用不会再次打开,因为从未到达 spawn.

That way, the first time the packaged app will be executed, the process.argv[1] will be empty, so the server won't start. It will then execute the electron part (mainLocal in my case) and start the app over, but this time passing the argv. Next time the app starts, it will start the server and stop the execution, so the app won't open again because spawn is never reached.

非常感谢塞缪尔.

这篇关于child_process.fork 没有在打包的电子应用程序内启动快速服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆