我写了一个简短的Electron Forge应用程序只运行一个快速网络服务器,在本地提供静态文件。为了可用性,我更喜欢直接运行 Node 进程。
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 消息。
知道我做错了什么吗?
最佳答案
我在开发中服务的实际上是相对于 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/