Mr.D
Mr.D

Reputation: 7893

Electron JS, start application with Express backend and with separate html front

I have an application which has separate ExpressJS backend and separate HTML front.

My ExpressJS backend is located in root/scripts/server.js

My HTML front is located in root/index.html. This front uses javascript logic, which was generated from webpack and it works correctly.

In order to make this project work I first launch my ExpressJS backend with this command:

babel-node ./scripts/server.js --presets es2015,stage-0

Then I open my index.html and everything works fine.

Now I want to pack both backend and front in one desktop application by using ElectronJS.

My project has main.js script which launches electron application in desktop mode. And it is located in root/main.js. It has following code:

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

const path = require('path')
const url = require('url')

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({
      width: 800, 
      height: 600,
      autoHideMenuBar: true,
      useContentSize: true,
      resizable: false
  })

  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, '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()
   }
})

This script launches only my HTML front. What do I need to add, in order to pack Express backend among with HTML front?

Upvotes: 2

Views: 2638

Answers (1)

Dennis W
Dennis W

Reputation: 961

Try adding something like this to main.js:

const server = require('./scripts/server');

Upvotes: 1

Related Questions