frankhale / electron-with-express

A simple app that demonstrates spawning an Express app from Electron
MIT License
662 stars 145 forks source link

How to use ipcRenderer from Express? #50

Closed nhannguyen512 closed 1 year ago

nhannguyen512 commented 1 year ago

How to use ipcRenderer from Express? Because I want to pass some Electron parameters to Express. For example: I want to pass the getAppPath path. Because I want to create a file in userPath. But from Express, I can't get userAppPath

frankhale commented 1 year ago

Have a look at the src/preload.ts file. It uses contextBridge to expose APIs to the renderer process.

image

nhannguyen512 commented 1 year ago

I see it, it worked on render file (app.ts) , but in Express.js (express-app.ts) , i can't use it .

router.get("/test", function (req: any, res: any, next) { // @ts-expect-error
let result : string = ipcRenderer.invoke("get-express-app-url"); res.json(result); });

==> ipcRenderer underfined

frankhale commented 1 year ago

preload.ts and thus ipcRenderer only work in the Electron renderer process (the process that renders the web pages). The Express app is spawned using Electron (running as a Node process) so it's running in a completely new process outside of the main Electron process. There are many ways to accomplish what you need. Look into alternative IPC solutions. The Express app has Node access, so does Electron main process so there are tons of ways to pass this information back and forth. You could even add it to a SQLite database as a way to pass data back and forth (that's probably not ideal but it would work).