首页 > 解决方案 > 在 Electron 应用程序中运行多个 nodejs 服务器

问题描述

我正在使用electron-builder为 macOS 打包 dmg 文件。但我需要同时运行两个 nodeJS 服务器。第一个效果很好,我像主文件一样使用它。但是第二个有问题,我尝试使用childProc.spawn(process.execPath, [path.join('path/to/server.js')]) . 当我调整此命令时,它会在我运行它时创建两个应用程序, electron .但是当我尝试使用此命令打包应用程序时,electron-builder会在启动时运行两个应用程序,然后关闭第二个应用程序(这是我产生的)。

如何在一个应用程序中运行两台服务器?

这是我的electron-builder.json

{
    "appId": "com.myApp.app",
    "productName": "myApp",
    "directories": {
        "app": ".",
        "output": "out",
        "buildResources": "build-res"
    },
    "files": [
        ...,
        "second-server-folder", 
    ],
    "mac": {
        "category": "public.app-category.utilities",
        "target": ["dmg"]
      },
      "dmg": {
        "background": null,
        "backgroundColor": "#ffffff",
        "window": {
            "width": "400",
            "height": "300"
        },
        "contents": [
            {
                "x": 100,
                "y": 100
            },
            {
                "x": 300,
                "y": 100,
                "type": "link",
                "path": "/Applications"
            }
        ]
    }

}

以及来自主文件的代码示例,我如何尝试运行第二台服务器:

childProc.spawn(process.execPath, ['path/to/server.js'])

以及`packge.json`的样本

{
  "name": "myApp",
  "version": "0.02.1",
  "private": true,
  "main": "bin/www",
  "scripts": {
    "start-electron": "electron .",
    "pack": "electron-builder build",
    "rebuild": "electron-rebuild",
  },
  "devDependencies": {
    "electron-rebuild": "^2.3.5",
    "electron": "^13.1.6",
    "electron-builder": "^22.10.5",
 
  }
}

标签: node.jselectronelectron-builder

解决方案


推荐阅读