简体   繁体   English

我创建了我的 react 应用程序并与 electron.js 连接。 现在如何将我的后端从服务器文件夹与电子连接

[英]I create a build of my react app and connected with electron.js. now how to connect my backend from server folder with electron

I create a build of my react app and connected with electron.js.我创建了我的 react 应用程序并与 electron.js 连接。 now how to connect my backend from server folder with electron.现在如何将我的后端从服务器文件夹与电子连接起来。

How can I add backend to electron .exe如何将后端添加到电子 .exe

const { app, BrowserWindow } = require('electron')
const path = require('path')
require('./app');
                
function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js')
        }
    })
                
    // win.loadFile('./demo.html')
    win.loadFile('./build/index.html')
}
                
app.whenReady().then(() => {
    createWindow()
    
    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow()
        }
    })
})
                
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

This is my folder structure这是我的文件夹结构

在此处输入图像描述

Check your routing first in the frontend file (like an index.js in react) if it is BrowserRouter then change it to HashRouter.首先在前端文件中检查您的路由(如 react 中的 index.js),如果它是 BrowserRouter,然后将其更改为 HashRouter。 Now copy the below code and paste it to the electron main file现在复制下面的代码并将其粘贴到电子主文件中

const { app, BrowserWindow } = require('electron')
const path = require('path')
const server = require('./app');

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // win.loadFile('./demo.html')
  win.loadURL('http://localhost:9000')
  win.loadURL('./build/index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})


// ******************************************************************************


/* const electron = require('electron')
const app = electron.app
handleSquirrelEvent(app)
const BrowserWindow = electron.BrowserWindow;
const path = require('path')
// const url = require('electron-is-dev');
const isDev = require('electron-is-dev');
let mainWindow;
// const createWindow = () => {
//     mainWindow = new BrowserWindow({ width: 800, height: 600 })
//     const appUrl = isDev ? 'http://localhost:3000' :
//         `file://${path.join(__dirname, '../build/index.html')}`
//     mainWindow.loadURL(appUrl)
//     // mainWindow.loadURL(url.format({
//     //     pathname: path.join(__dirname, '../build/index.html'),
//     //     protocol: 'file',
//     //     slashes: true
//     // }))
//     mainWindow.webContents.openDevTools()
//     mainWindow.on('closed', function () {
//         mainWindow = null
//     })
// }
// app.on('ready', createWindow)
// app.on('window-all-closed', function () {
//     if (process.platform !== 'darwin') {
//         app.quit()
//     }
// })
// app.on('activate', function () {
//     if (mainWindow === null) {
//         createWindow()
//     }
// })
const createWindow = () => {
  mainWindow = new BrowserWindow({ width: 480, height: 320 })
  const appUrl = isDev ? 'http://localhost:3000' :
    `file://${path.join(__dirname, '../build/index.html')}`
  mainWindow.loadURL(appUrl)
  mainWindow.maximize()
  mainWindow.setFullScreen(true)
  mainWindow.on('closed', () => mainWindow = null)
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
  // Follow OS convention on whether to quit app when
  // all windows are closed.
  if (process.platform !== 'darwin') { app.quit() }
})
app.on('activate', () => {
  // If the app is still open, but no windows are open,
  // create one when the app comes into focus.
  if (mainWindow === null) { createWindow() }
})
function handleSquirrelEvent(application) {
  if (process.argv.length === 1) {
    return false;
  }
  const ChildProcess = require('child_process');
  const path = require('path');
  const appFolder = path.resolve(process.execPath, '..');
  const rootAtomFolder = path.resolve(appFolder, '..');
  const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
  const exeName = path.basename(process.execPath);
  const spawn = function (command, args) {
    let spawnedProcess, error;
    try {
      spawnedProcess = ChildProcess.spawn(command, args, {
        detached: true
      });
    } catch (error) { }
    return spawnedProcess;
  };
  const spawnUpdate = function (args) {
    return spawn(updateDotExe, args);
  };
  const squirrelEvent = process.argv[1];
  switch (squirrelEvent) {
    // case '--squirrel-install': 58
    case '--squirrel-updated':
      // Optionally do things such as:
      // - Add your .exe to the PATH
      // - Write to the registry for things like file associations and
      //   explorer context menus
      // Install desktop and start menu shortcuts
      spawnUpdate(['--createShortcut', exeName]);
      setTimeout(application.quit, 1000);
      return true;
    case '--squirrel-uninstall':
      // Undo anything you did in the --squirrel-install and
      // --squirrel-updated handlers
      // Remove desktop and start menu shortcuts
      spawnUpdate(['--removeShortcut', exeName]);
      setTimeout(application.quit, 1000);
      return true;
    case '--squirrel-obsolete':
      // This is called on the outgoing version of your app before
      // we update to the new version - it's the opposite of
      // --squirrel-updated
      application.quit();
      return true;
    default:
      console.log("default case")
  }
};

 */

// ******************************************************************************

/* const electron = require('electron')
const path = require("path");
const url = require('url');
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow


process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({ width: 800, height: 600 })

  // and load the index.html of the app.
  // 'public' is the path where webpack bundles my app
  mainWindow.loadURL(`file://${__dirname}/public/index.html`);

  // Open the DevTools.
  mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// 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.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
}) */`enter code here`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何为我的 Electron 应用程序制作后端 - How would I make a backend for my Electron app 在我的 create-react-app 应用程序上运行电子构建会出现错误? - Running electron-build on my create-react-app app comes up with an error? 我的 Electron 应用程序未连接到服务器 - My Electron App is not connecting to server 如何在REACT中为连接到node.js的Axios路由创建一个单独的文件夹 - How can i create a separate folder in REACT for my Axios routes, that are connected to my node.js 尝试为我的电子应用程序创建安装程序时,生成未找到错误 - Build not found error when trying to create an installer for my electron app 我如何让Travis-CI为多个操作系统并行构建我的电子应用程序? - How can I have Travis-CI build my electron app for multiple OSes in parallel? 如何将 CSS 更改注入我的 Electron js 应用程序? - How to Inject CSS changes into my Electron js app? 如何在 Electron + React App 中创建路由 - How to create route in Electron + React App 如何从node.js服务器获取数据到Electron应用程序? - How to get data from node.js server into Electron app? 如何构建一个使用 React 构建的独立桌面应用程序,使用节点后端,并将前端和后端都包装在电子中作为桌面应用程序? - how to build a standalone desktop app built with react, using a node backend, and wrap both frontend and backend in electron as a desktop app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM