Bipin Shrestha
Bipin Shrestha

Reputation: 411

How to package express server inside electron app?

I am currently building electron app with vue-cli-electron-builder . I have mysql local database and express server.

How do I bundle express server and Electron app?

It would be perfect if I could package everything inside one app and still be able to access express server.

Running electron app and the separate express server works but I want to package both electron and express so that I can perform actions only with electron app.

Upvotes: 5

Views: 3233

Answers (1)

Polv
Polv

Reputation: 2208

In background.ts, you can import { fork } from 'child_process'.

And put server.js in /public/.

import { fork } from 'child_process'

const isDevelopment = process.env.NODE_ENV !== 'production'

const serverProcess = fork(isDevelopment 
  ? path.resolve(__dirname, "../public/server.js")
  : path.resolve(__dirname, "server.js"))

try {
  serverProcess.stdout!.on("data", console.log)
  serverProcess.stderr!.on("data", console.error)
} catch(e) {}

I have done it with vue-cli-electron-builder at some time as well, but it conflicts with Reveal.js, so I did it manually.

However, regarding MySQL, you shouldn't put .env or credentials in Electron, as people can reverse engineer it, needing a separate web server.

Upvotes: 3

Related Questions