电子构建应用程序无法启动Express

电子构建应用程序无法启动Express

本文介绍了电子构建应用程序无法启动Express Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,并且在开发人员模式下,所有程序都可以正常运行.Everythink可以正常工作.但是,当我用电子生成器打包我的应用程序时,应用程序打开但无法启动Express Server,并且应用程序无法正常运行.

I'm building an app and all works fine while I'm in developer mode. Everythink works as it should. But when I package my app with electron-builder, app opens but it doesnt start express server and app doesnt work properly.

这是我的package.json代码

Here is my package.json code

{
  "name": "artros",
  "version": "1.0.0",
  "description": "Artros",
  "author": "MC3",
  "license": "ISC",
  "main": "start.js",
  "scripts": {
        "pack": "build --dir",
        "dist": "build"
    },
    "build": {
        "appId": "com.artros.app",
        "productName": "Artros",
        "win": {
            "target": "portable",
            "icon": "build/icon.ico"
        },
        "mac": {
            "target": "dmg"
        }
    },
  "dependencies": {
    "body-parser": "^1.18.3",
    "ejs": "^2.5.7",
    "electron-pdf-window": "^1.0.12",
    "express": "^4.16.2",
    "multer": "^1.3.0",
    "nodemailer": "^4.6.4",
    "path": "^0.12.7"
  },
    "devDependencies": {
        "electron": "^1.8.2"
    }
}

这是我的start.js代码

and here is my start.js code

 const cluster = require('cluster');
    if (cluster.isMaster) {
      require('./main.js'); // your electron main file
      cluster.fork();
    } else {
      require('./app.js'); // your server code
    }

和我的main.js代码

and my main.js code

var electron = require('electron');
var browserWindow = electron.BrowserWindow;
var app = electron.app;

app.on('ready', function(){
    var appWindow;

//appWindow
    appWindow = new browserWindow({
        width:1120,
        height:620,
        webPreferences: {
            plugins: true
        },
        icon: __dirname + '/public/icon/icon.png'
    });
    appWindow.loadURL('file://' +__dirname + '/public/prva.html');

    //appWindow.webContents.openDevTools();
});

// close app after all windows are closed
    app.on('window-all-closed', () => {
  app.quit()
})

如果有人知道问题出在哪里,请发布它.谢谢

If anybody has any idea what is the problem, please post it. Thanks

推荐答案

我也遇到了类似的情况.挑战在于,如果您使用fork(),则应用程序路径会更改.因此,我建议您检查所有文件中的 __ dirname ,尤其是分叉进程(例如app.js)中的文件.如果其中一些不再有意义,我不会感到惊讶.

I had something similar happen to me. The challenge was that if you use fork() the application path changes. So I would recommend that you check __dirname in all of your files especially the ones in your forked process (e. g. app.js). I wouldn't be surprised if some of them don't make sense anymore.

这篇关于电子构建应用程序无法启动Express Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:18