Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js child_process.fork未在打包的electron应用程序内启动express server_Node.js_Express_Electron_Child Process - Fatal编程技术网

Node.js child_process.fork未在打包的electron应用程序内启动express server

Node.js child_process.fork未在打包的electron应用程序内启动express server,node.js,express,electron,child-process,Node.js,Express,Electron,Child Process,我有一个electron应用程序,我不仅需要运行用户界面,还需要启动一个express服务器,为通过网络连接的人提供文件 如果我能正常启动electron和express服务器,我的一切都能正常工作,但我很有信心,我需要服务器在不同的线程中运行,以避免界面迟缓,甚至服务器出现问题 为此,我尝试使用child_process.fork运行我的express server,当我使用npm start时,它工作正常,但当我使用electron builder创建.exe时,安装的程序不会启动expre

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

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

为此,我尝试使用child_process.fork运行我的express server,当我使用
npm start
时,它工作正常,但当我使用
electron builder
创建.exe时,安装的程序不会启动express server

我尝试立即使用以下工具运行服务器:

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

我尝试了一些更改,在文件前面加上
\uuu dirname
process.resourcesPath
,甚至对生成的文件路径进行硬编码;将fork选项更改为pass
cwd:uu dirname
distached:true
stdio:“忽略”
;甚至还尝试将
spawn
process.execPath
一起使用,它也可以与
npm start
一起使用,但打包后不会使用(它会不断打开我的应用程序的新实例,在你使用hehe之后似乎很明显)

注意:如果我没有立即使用fork和require服务器脚本,那么使用
require('server/mainServer.js')
它可以在打包的应用程序上工作,因此最类似的问题不是express本身

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

我提出了一个小git项目来说明我的问题:


任何帮助都将不胜感激。

在塞缪尔·阿塔德()的大力帮助下,我终于解决了这个问题(事实上他为我解决了)

正如他所说:

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
实际上是使用
process.execPath
执行
spawn
,并将fork的第一个参数作为spawn的第二个参数传递

打包应用程序中发生的情况是,
process.execPath
不是electron,而是打包应用程序本身。因此,如果您尝试生成,应用程序将被一次又一次地打开

因此,Samuel的建议是这样实施的:

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

require('./local/mainLocal.js')
require('child_process').spawn(process.execPath, ['--start-server'])
这样,第一次执行打包的应用程序时,
进程.argv[1]
将为空,因此服务器不会启动。然后它将执行electron部分(在我的例子中是mainLocal)并重新启动应用程序,但这次传递的是
argv
。下一次应用程序启动时,它将启动服务器并停止执行,因此应用程序将不会再次打开,因为从未到达spawn

非常感谢塞缪尔