electron-forge – express not running in final package

huangapple go评论70阅读模式
英文:

electron-forge - express not running in final package

问题

I have an Electron application based on the electron-webpack-typescript boilerplate. This application also includes an Express server. Everything works fine when I use the command yarn start for testing, but when I use the command yarn package to create the final build, the Express server doesn't start. The address where the server should run, localhost:5020, returns "This site can't be reached."

Building is provided by electron-forge:

    "start": "electron-forge start",
    "package": "electron-forge package",

index.ts

import { app, BrowserWindow } from 'electron';
import { io } from 'socket.io-client';

declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
let mainWindow: BrowserWindow;

if (require('electron-squirrel-startup')) {
  app.quit();
}

const createWindow = (): void => {
  mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: true,
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
    },
  });

  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
  mainWindow.webContents.openDevTools();
};

app.on('ready', () => {
  createWindow();
  startSocketIO();
  runServer();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

import { runServer } from './server';

function startSocketIO() {
  const socket = io(process.env.SERVER_URL);

  socket.on('connect', () => {
    console.log('Connected to Socket.io server');
    socket.emit('chat message', 'Test message from Electron');
  });

  socket.on('command', (data) => {
    mainWindow.webContents.send('data', data);
  });

  socket.on('disconnect', () => {
    console.log('Disconnected from Socket.io server');
  });
}

server.ts

import express from 'express';
import http from 'http';
import { Server } from 'socket.io';

import dotenv from 'dotenv';
dotenv.config();

const app = express();
const server = new http.Server(app);
const io = new Server(server);

export function runServer(): void {
  app.get('/', (req, res) => {
    if (req.headers.cmd !== undefined) {
      io.emit('command', {
        created: new Date().toLocaleString('pt-BR').replace(',', ''),
        command: atob(req.headers.cmd as string),
        parameters: atob(req.headers.cmddata as string)
      });
    }
    res.send('SERVER RUNNING!')
  });

  server.listen(process.env.SERVER_PORT, () => {
    console.log(`Socket.IO server running at http://localhost:${process.env.SERVER_PORT}/`);
  });
}

.env

SERVER_URL=http://localhost:5020
SERVER_PORT=5020

I tried to move all variables from .env directly to the code - no changes. I also tried to move some of my dependencies in package.json from dependencies to devDependencies - also no changes.

// UPDATE 17.05.2023 [15:33]

I discovered that when I move the code:

server.listen(process.env.SERVER_PORT, () => {
  console.log(`Socket.IO server running at http://localhost:${process.env.SERVER_PORT}/`);
});

from server.ts directly to the index.ts, everything works properly! So there must be a problem with my server.ts definition or in the import in index.ts.

英文:

I have an Electron application based on the electron-webpack-typescript boilerplate. This application also includes an Express server. Everything works fine when I use the command yarn start for testing, but when I use the command yarn package to create the final build, the Express server doesn't start. The address where the server should run, localhost:5020, returns "This site can't be reached."

Building is provided by electron-forge:

    "start": "electron-forge start",
"package": "electron-forge package",

index.ts
<!-- language: lang-js -->

import { app, BrowserWindow } from &#39;electron&#39;;
import { io } from &#39;socket.io-client&#39;;
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
let mainWindow: BrowserWindow;
if (require(&#39;electron-squirrel-startup&#39;)) {
app.quit();
}
const createWindow = (): void =&gt; {
mainWindow = new BrowserWindow({
height: 600,
width: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
});
mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
mainWindow.webContents.openDevTools();
};
app.on(&#39;ready&#39;, () =&gt; {
createWindow();
startSocketIO();
runServer();
});
app.on(&#39;window-all-closed&#39;, () =&gt; {
if (process.platform !== &#39;darwin&#39;) {
app.quit();
}
});
app.on(&#39;activate&#39;, () =&gt; {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
import { runServer } from &#39;./server&#39;;
function startSocketIO() {
const socket = io(process.env.SERVER_URL);
socket.on(&#39;connect&#39;, () =&gt; {
console.log(&#39;Connected to Socket.io server&#39;);
socket.emit(&#39;chat message&#39;, &#39;Test message from Electron&#39;);
});
socket.on(&#39;command&#39;, (data) =&gt; {
mainWindow.webContents.send(&#39;data&#39;, data);
});
socket.on(&#39;disconnect&#39;, () =&gt; {
console.log(&#39;Disconnected from Socket.io server&#39;);
});
}

server.ts
<!-- language: lang-js -->

import express from &#39;express&#39;;
import http from &#39;http&#39;;
import { Server } from &#39;socket.io&#39;;
import dotenv from &#39;dotenv&#39;;
dotenv.config();
const app = express();
const server = new http.Server(app);
const io = new Server(server);
export function runServer(): void {
app.get(&#39;/&#39;, (req, res) =&gt; {
if(req.headers.cmd !== undefined) {
io.emit(&#39;command&#39;, {
created: new Date().toLocaleString(&#39;pt-BR&#39;).replace(&#39;,&#39;, &#39;&#39;),
command: atob(req.headers.cmd as string),
parameters: atob(req.headers.cmddata as string)
});
}
res.send(&#39;SERVER RUNNING!&#39;)
});
server.listen(process.env.SERVER_PORT, () =&gt; {
console.log(`Socket.IO server running at http://localhost:${process.env.SERVER_PORT}/`);
});
}

.env

SERVER_URL=http://localhost:5020
SERVER_PORT=5020

I tried to move all variables from .env directly to the code - no changes
I also tried to move some of my dependencies in package.json from dependencies to devDependencies - also no changes

// UPDATE 17.05.2023 [15:33]

I discovered that when I move the code:

  server.listen(process.env.SERVER_PORT, () =&gt; {
console.log(`Socket.IO server running at http://localhost:${process.env.SERVER_PORT}/`);
});

from server.ts directly to the index.ts, everything works properly! So there must be problem with my server.ts definition or in import in index.ts

答案1

得分: 0

以下是您要翻译的内容:

找到解决方法!

我发现问题出在 dotenv 上。出于某种原因,通过 electron-forge 创建的生产构建无法导出 .env 变量。在开发模式下是可以的。我尝试了谷歌可能的解决方法,但不幸的是,我什么都没找到。

所以我创建了自己的 config.ts 文件,内容如下:

const APP_CONFIG = {
  SERVER_PORT: 5020,
  SERVER_URL_BASE: 'http://localhost',
  SERVER_URL: () => {
    return `${APP_CONFIG.SERVER_URL_BASE}:${APP_CONFIG.SERVER_PORT}`;
  }
}

export default APP_CONFIG;

然后我通过以下方式引入这个文件:

import APP_CONFIG from '../config'
.
.
.
.
.
.
.
server.listen(APP_CONFIG.SERVER_PORT, () => {
  console.log(`Socket.IO server running at ${APP_CONFIG.SERVER_URL()}`);
});
英文:

SOLUTION FOUND!

I found that the problem was in the dotenv. For some reason, production build created via electron-forge can't export .env variables. In development mode it's possible. I tried to Google possible way how to do this but unfortunately I didn't found anything.

So I created my own config.ts file, which looks like this:

const APP_CONFIG = {
SERVER_PORT: 5020,
SERVER_URL_BASE: &#39;http://localhost&#39;,
SERVER_URL: () =&gt; {
return `${APP_CONFIG.SERVER_URL_BASE}:${APP_CONFIG.SERVER_PORT}`;
}
}
export default APP_CONFIG;

and then I am including this file by:

import APP_CONFIG from &#39;../config&#39;
.
.
.
.
.
.
server.listen(APP_CONFIG.SERVER_PORT, () =&gt; {
console.log(`Socket.IO server running at ${APP_CONFIG.SERVER_URL()}`);
});

huangapple
  • 本文由 发表于 2023年5月17日 15:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定