node.js - 从 Electron-forge 应用程序中使用 Express 提供静态文件

标签 node.js express electron electron-forge

我写了一个简短的Electron Forge应用程序只运行一个快速网络服务器,在本地提供静态文件。为了可用性,我更喜欢直接运行 Node 进程。

ma​​in.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 消息。

知道我做错了什么吗?

最佳答案

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

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

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

关于node.js - 从 Electron-forge 应用程序中使用 Express 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909756/

相关文章:

ajax - 获取终端命令(Nodejs)并通过 AJAX 发送到特定端口

javascript - 未处理的 promise 拒绝警告 : TypeError: Converting circular structure to JSON with Jest + Angular

javascript - Node.js 应用、Express 和 Cloud Foundry

node.js - nodejs ExpressJs BackboneJs Pushstate

electron - 在 Electron 应用程序的安装过程中注册自定义协议(protocol)

javascript - Express JS - 将后端变量/数据传递给 Angular JS

javascript - 服务前处理 Cookie

javascript - 仅当数组中尚不存在某些内容时才添加到数组中?

node.js - Electron/NodeJS 和应用程序在 setInterval/async 代码上卡住

electron - Electron 预加载器脚本中的访问路径如何?