Any reason to use web server (Express) within Electron app?
Asked Answered
P

2

8

I'm learning Electron and everything I'm seeing is using the file:// protocol to load pages, and so far this is working fine. I also see some references to using Express within Electron.

My question is - is there any reason to use a web server such as Express within an Electron app? What does it get you?

Thanks.

Palenque answered 30/12, 2016 at 0:10 Comment(0)
C
4

I think the scenario is pretty odd: The combination of a desktop-UI with a server-framework seems to be somewhat counter-intuitive.

What you see when file:// is referenced are (local) file system calls - these could well be calls to other protocols like http:// or ws:// instead, and do not require the Express framework to be present.

Instead, Express enables your application to receive connections from the outside and act as a server. This could be a webserver serving static or dynamic content, a REST-API endpoint or some other kind of web service endpoint.

There is indeed a project showing exactly this combination: The Express server is responsible for serving content, Electron is used to wrap a logging-UI that displays whatever is currently happening.

From an architecturial standpoint however, I would probably seperate each of these concerns into seperate standalone applications.

Cursor answered 30/12, 2016 at 7:31 Comment(4)
Thanks, Jens. I get the client/server distinction. Are you saying that within Electron I can use http without express? I'm not aware of any benefit to doing so, but that's kinda the crux of my question...am I missing some benefit to using http:// over file:// ?Palenque
The file:// call will always be a call from the Electron application to some file in the system, while using Express will enable http calls to the Electron application from outside the scope of the application itself (using other clients, like browsers)Cursor
Can you explain more why it is somewhat counter-intuitive?Paternal
There are absolutely valid use-cases for using express and electron alongside each other. For simple apps it's absolutely not necessary, but it may improve re-usability for more complex apps. * Electron for desktop UI, Express for existing backend API * Companion desktop app for Express-powered site * Add Express APIs to enhance Electron app functionality * Share UI code between Electron and Express-served web version The key distinction is separating the UI layer from the API/server layer.Cursor
P
2

I was considering this since I'm making an Offline Desktop Electron app and wanted to make some reusable functions for an app that could be a new SaaS solution.

So unless you were thinking of the same, then I don't really find any good reason to have this. I'm trying to find some best practices, but I found little to none. It seems the practice was quite uncommon although totally possible. Technically, all you have to do is use the Electron as a server.

At first, I tried out ExpressJS just to make sure whether the express app could run, the only problem I had was with cors, but things went well after that. What I can see is, that if you're planning to create a desktop app, and it was about to go complex, you should just go to your usual web library/framework tools to make it easier.

But hey, I'm open to suggestions if I made a mistake okay. I'm trying out this myself. I'll update anytime if I found the cons of it.

Paternal answered 27/12, 2023 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.