在Electro-forge应用程序中使用Express服务静态文件

时间:2019-11-18 07:28:44

标签: node.js express electron electron-forge

我编写了一个快速的Electron Forge应用程序,该应用程序只运行一个在本地提供静态文件的快速Web服务器。我希望这样做比直接运行节点进程来提高可用性。

main.js

import { app, BrowserWindow } from 'electron';
import express from 'express';

const exApp = express();
exApp.use(express.static('web-app'));
exApp.listen(3333);

let mainWindow;

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
  // ...
  });

  // ...
};

// ...

我使用CopyWebpackPlugin将需要提供的文件复制到.webpack/main/web-app/目录中。

webpack.main.config.js

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: './src/main.js',
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  plugins: [
    new CopyPlugin([
      { from: path.resolve(__dirname, 'web-app'), to: 'web-app' }
    ]),
  ]
};

这在开发中效果很好(通过yarn start)。

当我尝试运行yarn make时,它成功构建了应用程序并生成了可运行的exe,但是在运行应用程序后尝试访问http://localhost:3333/时,会显示Cannot GET / 404消息。

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

我在开发中使用的实际上是相对于节点进程的web-app目录,而不是复制到.webpack/...中的目录。

为了在生产中提供适当的文件,我将exApp.use()行更改为:

exApp.use(express.static('resources/app/.webpack/main/web-app'));