从 electron-forge 应用程序中使用 express 服务静态文件
Serve static files with express from within electron-forge app
我编写了一个快速 Electron Forge 应用程序,它只是 运行 一个在本地提供静态文件的快速网络服务器。我更喜欢这个而不是 运行直接使用节点进程。
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,但在 [=39= 之后尝试访问 http://localhost:3333/
] 应用程序,导致 Cannot GET /
404 消息。
知道我做错了什么吗?
我在开发中服务的实际上是相对于节点进程的 web-app
目录,而不是复制到 .webpack/...
.
的目录
为了在生产中提供正确的文件,我将 exApp.use()
行更改为:
exApp.use(express.static('resources/app/.webpack/main/web-app'));
我编写了一个快速 Electron Forge 应用程序,它只是 运行 一个在本地提供静态文件的快速网络服务器。我更喜欢这个而不是 运行直接使用节点进程。
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,但在 [=39= 之后尝试访问 http://localhost:3333/
] 应用程序,导致 Cannot GET /
404 消息。
知道我做错了什么吗?
我在开发中服务的实际上是相对于节点进程的 web-app
目录,而不是复制到 .webpack/...
.
为了在生产中提供正确的文件,我将 exApp.use()
行更改为:
exApp.use(express.static('resources/app/.webpack/main/web-app'));