简体   繁体   English

使用电子锻造应用程序中的快递服务 static 文件

[英]Serve static files with express from within electron-forge app

I've written a quick Electron Forge app that simply runs an express webserver that serves static files locally.我编写了一个快速的Electron Forge应用程序,它只运行一个快速网络服务器,在本地提供 static 文件。 I prefer this to running a node process directly for usability.为了可用性,我更喜欢直接运行节点进程。

main.js 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({
  // ...
  });

  // ...
};

// ...

I use the CopyWebpackPlugin to copy the files I need to serve into the .webpack/main/web-app/ directory.我使用CopyWebpackPlugin将需要提供的文件复制到.webpack/main/web-app/目录中。

webpack.main.config.js 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' }
    ]),
  ]
};

This works perfectly in development (via yarn start ).这在开发中非常有效(通过yarn start )。

When I try to run yarn make , it successfully builds the app and generates a runnable exe, but trying to access http://localhost:3333/ after running the app, results in a Cannot GET / 404 message.当我尝试运行yarn make时,它成功构建了应用程序并生成了一个可运行的 exe,但在运行应用程序后尝试访问http://localhost:3333/时,会导致Cannot GET / 404 消息。

Any idea what I'm doing wrong?知道我做错了什么吗?

What I was serving in development was actually the web-app directory relative to the node process, and not the one copied into .webpack/... .我在开发中服务的实际上是相对于节点进程的web-app目录,而不是复制到.webpack/...的目录。

To serve the proper files in production, I changed the exApp.use() line to:为了在生产中提供正确的文件,我将exApp.use()行更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM