首页 > 解决方案 > 应用级拦截的电子代理设置

问题描述

我正在尝试设置一个代理服务器来代理我的电子应用程序中的每个请求,但我似乎无法让请求通过代理。我遵循了文档,我认为这是正确的,但我不知道它是否有效(我在日志中看不到任何内容)

(async () => {
    const getPort = require('get-port');
    const { setupProxyServer, stopProxyServer } = require('./proxy');
    const instance = 'http://localhost:8080';

    const PROXY_PORT = await getPort();
    await setupProxyServer(instance, PROXY_PORT);

    // Modules to control application life and create native browser window
    const { app, BrowserWindow, session } = require('electron')
    
    app.commandLine.appendSwitch('proxy-server', PROXY_PORT);

    const createWindow = async () => {
        // Create the browser window.
        const mainWindow = new BrowserWindow({
            width: 1800,
            height: 1200
        })
        
        // Open the DevTools.
        if (process.env.DEVTOOLS) mainWindow.webContents.openDevTools();

        // and load the index.html of the app.
        mainWindow.loadURL(instance);
    };

    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.whenReady().then(async () => {
        // session.fromPartition('persist:glide');

        createWindow();

        app.on('activate', () => {
            // On macOS it's common to re-create a window in the app when the
            // dock icon is clicked and there are no other windows open.
            if (BrowserWindow.getAllWindows().length === 0) createWindow()
        });
    });

    // Quit when all windows are closed, except on macOS. There, it's common
    // for applications and their menu bar to stay active until the user quits
    // explicitly with Cmd + Q.
    app.on('window-all-closed', async () => {
        if (process.platform !== 'darwin') app.quit()

        await stopProxyServer();
    });
})();

中间件设置很好我可以在日志中看到中间件服务器已启动并等待请求。

标签: javascriptelectron

解决方案


推荐阅读