frankhale / electron-with-express

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

Question: Why use a html to load the server file? #46

Closed rafinskipg closed 1 year ago

rafinskipg commented 1 year ago

Hello, i wonder why do you need to use a index.html to load the server.js file?

I'm trying to do something similar with nextjs but instead i'm loading the server on the main.js file.

It works in development mode, but not on the bundled version, and i don't know why.

Came here to ask to see your reasoning and see if it helps me

frankhale commented 1 year ago

I think the answer is because I always saw it done that way and the Electron documentation mentioned to do it that way. I didn't even consider just doing everything in the main.js file. I can't think of any technical reasons right off the top of my head that would require it to use the index.html and load the server.js code the way it's currently doing it.

rafinskipg commented 1 year ago

@frankhale

After my digging:

If you bundle an app with electron-builder you won't be able to use any custom server, only load static html files. Maybe that is why you have it done that way.

There are ways to enable communication with the node server via IPC and/or disable some security flags. But is not advised to use that.

i whish electron supported node servers directly but it seems that is a security risk for some reason.

frankhale commented 1 year ago

If you bundle an app with electron-builder you won't be able to use any custom server, only load static html files. Maybe that is why you have it done that way.

To be honest I've never once touched electron-builder and when I originally wrote this example I had always imagined it'd be geared towards people who are writing custom things for themselves and not for distribution. I can see why electron-builder would take a security stance like this but for me I think it's okay to run a server like this and serve electron the app. It makes a lot of sense and it can be used to do all sorts of really cool things. Maybe there is a way to package the app in a different way that will be similar to electron-builder but not be limited to static HTML.

rafinskipg commented 1 year ago

I feel you. I think the same. The cool thing about electron is you can run your own stuff on the desktop easily. But you need the builder to export installable versions if you want other users to use it. I believe there are ways to circumvent the limitation by enabling some flags and allowing to use node.

https://www.electronjs.org/docs/latest/api/webview-tag#nodeintegration https://stackoverflow.com/questions/66455289/unable-to-use-node-js-apis-in-renderer-process https://www.electronjs.org/docs/latest/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content

frankhale commented 1 year ago

This just popped up on my Github recommendations: https://github.com/jlongster/electron-with-server-example

It's another example of Electron with a server. Might be of use to those that want to try different ways to do it.

rafinskipg commented 1 year ago

Thanks! @frankhale , good suggestion :)

frankhale commented 1 year ago

I'm going to be doing some work to this soon and am going to be looking for ways to simplify it. I like your suggestion about doing more in the main.js so that's definitely going to be something I want to look at.

frankhale commented 1 year ago

I've reworked the entire codebase to make it simpler and put the spawning of Express in main.ts.

Commit: d766f7ec4e5e4a9384db23d3fa5b4223e8d720f5

frankhale commented 1 year ago

After my digging: You can write the server directly on main.js , and run electron main.js, and it will work. BUT 💣 this only works on development, where electron has access to the node context. If you bundle an app with electron-builder you won't be able to use any custom server, only load static html files. Maybe that is why you have it done that way.

I've removed the dependency directly on Node and now run Electron with the ELECTRON_RUN_AS_NODE flag to spawn the Express server. I've also done the leg work to make it work if you package using electron-packager. I have not looked into electron-builder but I suspect it will work as well because there is no problem spawning the express server when the app is packaged as an asar.