在Linux上使用Asar运行电子应用程序/无法获取index.html

问题描述

我在/app/resources/app.asar下构建了一个带有asar捆绑包的电子应用程序。当我在linux中运行该应用程序时,我看到该应用程序无法启动,并抛出 / Cannot GET index.html

此应用程序是为在电子框架内运行的nodejs服务器提供角度分发服务而构建的。

这是在packge.json中构建应用的方式

 "builderForLinx1": "electron-packager --out linx64 --overwrite --platform linux --appname myApp .  --executable-name ClientSettings --asar" 

这是main.js调用的app.js(nodejs条目)

const { app,browserWindow } = require('electron')
const path = require('path');
const serverJS = require('./app');//node server

let win;

function createWindow () {
  // Create the browser window.
  win = new browserWindow({
    webPreferences: {
      nodeIntegration: true,webviewTag: true
    },show:false
  })

  
  win.loadURL("http://localhost:4440/index.html");
  win.setMenuBarVisibility(false);

  win.on('closed',function () {
    win = null
  })

  win.once('ready-to-show',() => {
    win.show();
    win.maximize();
  })
}

app.on('ready',createWindow)
**app.js**
var express = require('express')
var app = express()
var http = require('http');
var path = require('path');
const routes = require('./server/index.route');
const bodyParser = require('body-parser');

let port = 4440;
app.set('port',port);
let server;
server = http.createServer(app);
server.on('listening',onListening);
server.on('error',onError);

function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'?'pipe ' + addr: 'port ' + addr.port;
    console.log('Listening on ' + bind);
  }
server.listen(port);


app.use( express.static(path.join(__dirname,'dist/Angular-electron')))

// respond with "hello world" when a GET request is made from homepage
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'?'Pipe ' + port:'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}


routes(app);
module.exports = app;

但是,如果我不使用-asar来构建应用程序,则该应用程序将按预期启动!我这样做只是为了减少文件内容。这是要求的一部分。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)