D
D
Daniil Sugonyaev2019-03-03 00:16:40
Node.js
Daniil Sugonyaev, 2019-03-03 00:16:40

How to start node server when electron app starts?

I made a small application on Electron, but I can't figure out how to run a node server along with the application.
Electron.js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

const path = require('path');
const url = require('url');
const isDev = require('electron-is-dev');

let mainWindow;

function createWindow() {
    mainWindow = new BrowserWindow({width: 1400, height: 880});
    mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
    mainWindow.on('closed', () => mainWindow = null);
}

app.on('ready', createWindow);

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

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow();
    }
});

Found a tip for using require inside the startup file electron. But it constantly gives an error that it did not find the node file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cr2ed, 2019-03-04
@Cr2ed

Do this with the npm command in the scripts section of the package.json file. You will have something like

...
scripts : {
    'start-all': 'node server && electron .'
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question