node.jsexpresselectron

Set up a server from within an electron app


I haven't had any success looking for this because I mostly find misleading questions, about people wanting to use data from a server inside of their electron app. That's not my case.

I have a regular app, which uses a server on the internet, just like any other, but we want to make it available for schools without internet (without any or without reliable internet), so what I'm trying to do is to create a version of my server which runs from an electron exe and serves files for the students conected to the wifi (but no the internet) to access. After the process is done "offline", I will sync the data from the electron app itself.

I tried to run a server from express but I didn't have any progress so far. What I tried was to put the exact same code from my node server in my main.js file and had no luck.

I know that's not what electron is supposed to do, if you're positively sure there is no way to do that, please tell me so I can search for another alternative.


Solution

  • A simple approach is to create a cluster where the master process is the Electron Main and the worker process is the server.

    Example:

        const cluster = require('cluster');
        if (cluster.isMaster) {
          require('./main.js'); // your electron main file
          cluster.fork(); 
        } else {
          require('./server.js'); // your server code
        }