1

i have a problem to connect with server when using electron builder so i guess the electron cant find the server file server.js in the root there is client server folders and main.js package.json

main.js

const { app, BrowserWindow } = require("electron");
const path = require("path");
const { spawn } = require("child_process");
const url = require("url");

let mainWindow;
let serverProcess;

// Helper function to get the correct path for different platforms
function getIndexPath() {
  return path.join(__dirname, "client", "dist", "index.html");
}

// Function to create the main window
function createWindow() {
  // Create Electron window
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
    },
  });

  // Load the React app using a file URL (this is crucial for proper path resolution)
  const indexPath = getIndexPath(); // Get the path dynamically
  mainWindow.loadURL(
    url.format({
      pathname: indexPath,
      protocol: "file:",
      slashes: true,
    })
  );

  // For debugging
  mainWindow.webContents.openDevTools();

  // Handle window close
  mainWindow.on("closed", () => {
    mainWindow = null;
  });
}

// Function to start the backend server
function startServer() {
  const serverScript = path.join(__dirname, "server", "src", "server.js");
  const serverProcess = spawn("node", [serverScript]);

  serverProcess.stdout.on("data", (data) => {
    console.log(`Server stdout: ${data}`);
  });

  serverProcess.stderr.on("data", (data) => {
    console.error(`Server stderr: ${data}`);
  });

  serverProcess.on("close", (code) => {
    console.log(`Server process exited with code ${code}`);
  });

  return serverProcess;
}

// On app ready, start the server and create the window
app.on("ready", () => {
  console.log("Starting application...");

  // Start the Node.js backend server
  serverProcess = startServer();

  // Create the Electron window
  createWindow();
});

// Quit the app when all windows are closed (except on macOS)
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    if (serverProcess) {
      serverProcess.kill();
    }
    app.quit();
  }
});

// Recreate window on activate (macOS behavior)
app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});

// Handle cleanup on quitting the app
app.on("will-quit", () => {
  if (serverProcess) {
    serverProcess.kill();
  }
});

server.js

import express from "express";
import cors from "cors";
import { fetchPlc, insertData } from "./utils/db.js";

const app = express();
app.use(
  cors({
    origin: "*",
  })
);
app.use(express.json());
app.get(`/schema/:plc`, (req, res) => {
  const { plc } = req.params;
  const operandeTableData = fetchPlc(plc);
  return res.send(operandeTableData);
});
app.post(`/schema`, (req, res) => {
  const result = insertData(req.body.jsonData, req.body.ImportType, req.body.plc);

  if (result.success) {
    return res.status(200).json({ message: "Data inserted successfully" });
  } else {
    return res.status(500).json(result); // Send the error details as JSON
  }
});

app.listen(5000, () => {
  console.log("server is running on port 5000");
});

package.json

{
  "name": "fofo",
  "version": "1.0.0",
  "main": "main.js",
  "description": "Your app description",
  "author": {
    "name": "Your Name",
    "email": "[email protected]"
  },
  "scripts": {
    "start": "electron .",
    "build": "electron-builder"
  },
  "devDependencies": {
    "electron": "^35.0.3",
    "electron-builder": "^25.1.8"
  },
  "dependencies": {
    "child_process": "^1.0.2",
    "electron-is-dev": "^3.0.1"
  },
  "build": {
    "appId": "com.sonasid.fofo",
    "productName": "fofo",
    "directories": {
      "output": "dist"
    },
    "files": [
      "client/dist/**/*",
      "server/**/*",
      "main.js",
      "package.json"
    ],
    "extraResources": [
      {
        "from": "data.sqlite",
        "to": "data.sqlite"
      }
    ],
    "win": {
      "target": "nsis"
    },
    "linux": {
      "target": "deb",
      "category": "Utility"
    },
    "mac": {
      "target": "dmg"
    }
  }
}

project-root/
├── client/
│   └── dist/
│       └── index.html
├── server/
│   └── src/
│       ├── server.js
│       └── utils/
│           ├── db.js
│           └── other-utility-files.js
├── data.sqlite
├── main.js
├── package.json
├── node_modules/
└── dist/  (output directory for build artifacts react)

package.json

{
  "name": "fofo",
  "version": "1.0.0",
  "main": "main.js",
  "description": "Your app description",
  "author": {
    "name": "Your Name",
    "email": "[email protected]"
  },
  "scripts": {
    "start": "electron .",
    "build": "electron-builder"
  },
  "devDependencies": {
    "electron": "^35.0.3",
    "electron-builder": "^25.1.8"
  },
  "dependencies": {
    "child_process": "^1.0.2",
    "electron-is-dev": "^3.0.1"
  },
  "build": {
    "appId": "com.sonasid.fofo",
    "productName": "fofo",
    "directories": {
      "output": "dist"
    },
    "files": [
      "client/dist/**/*",
      "server/**/*",
      "main.js",
      "package.json"
    ],
    "extraResources": [
      {
        "from": "data.sqlite",
        "to": "data.sqlite"
      }
    ],
    "win": {
      "target": "nsis"
    },
    "linux": {
      "target": "deb",
      "category": "Utility"
    },
    "mac": {
      "target": "dmg"
    }
  }
}

i tried to resolve correctly the path but nothing work . the app work but i need to manually start the server .also i had problem with platforms`

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.