electron-packager打包后找不到http-proxy-middleware

重度胡癌患者
  • 110

因项目要求现在需要将现有的react项目打包成.exe文件。我自己先尝试打包成.app文件,因为本机是mac。

打包命令如下

"electron-build-mac": "electron-packager . appName --platform=darwin --arch=x64 --icon=./build/favicon.png --out=./desk/mac --app-version=1.0.0 --overwrite --ignore=\"(scripts|src|docs|.gitignore|LICENSE|README.md|public|config|public|node_modules|desk)\"",

打包出来现在报错信息如下

image.png

main.js如下

const { app, BrowserWindow } = require('electron');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const express = require('express');
const application = express();
const START_PORT = 50001;
const DOMAIN = 'xxx';

function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 1920,
        height: 1000,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
        },
    });

    proxys().then((res) => {
        // mainWindow.loadURL(`http://localhost:${START_PORT}`);
        mainWindow.loadURL(`http://127.0.0.1:${START_PORT}`);
        mainWindow.webContents.openDevTools();
    });
}

function proxys() {
    return new Promise((resolve, reject) => {
        application.use(
            createProxyMiddleware('/api', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/v1', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/icons', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/apks', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/zip', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/img_avatar', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/screenshot', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/data', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/android', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/ipa_icons', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/ipas', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/admin', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        application.use(
            createProxyMiddleware('/ws', {
                target: DOMAIN,
                changeOrigin: true,
                secure: false,
            })
        );
        // 通常用于加载静态资源
        application.use(express.static('build'));
        application.get('*', function (request, response) {
            response.sendFile(path.resolve(__dirname, 'build', 'index.html'));
        });
        application.listen(START_PORT, () => {
            resolve();
        });
    });
}
app.whenReady().then(() => {
    createWindow();
    app.on('activate', function () {
        if (BrowserWindow.getAllWindows().length === 0) createWindow();
    });
});

app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit();
});
回复
阅读 292
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏