Connecting Electron to REST endpoint (especially Express / MongoDB)?


#1

I have a front end Electron app, and separately have a REST-style server/database (I have used Express and MongoDB). I would like to connect the two. So an action in the electron app can interact with the Express server/Mongo database, such as executing a POST action and creating a new record in the database. I am having trouble finding guidance. Is there a clear way of doing this? Or at least some Electron methods to try out?

Some more details: To use a basic example: say I want to use Electron to build a chat desktop app. I would like two users (on separate machines) to be able to chat with each other, and then have the data of their chat persisted on an external database. To do this in Electron, the closest I have found is to create a separate web app within the Electron shell using mainWindow.loadURL(’[my url]’), probably assisted by the webview tag.

Haven’t gotten that to work, but I would prefer another way anyway. I would like to avoid loading a separate website, and instead have all the frontend be in Electron, with just certain actions within Electron (like uploading a chat message) then hitting a server/database, and being relayed to other clients.

I assume like how Slack must do it in their desktop app, or how I would think other Electron-built apps must work that require persisting data, such as Nylas’ N1 email app.

Any suggestions? Thanks.


#2

Maybe use the http.request from the bundled node.js ?


#3

Thanks for the suggestion. It had occurred to me that a fairly straightforward way to do what I want is probably to have an action in the Electron front end (say, clicking ‘submit’) causing an AJAX POST call to the endpoint set up by Express / MongoDB backend. The node http request module, like you mention, seems like a potentially good alternative to AJAX for doing this, so I will look into it.


#4

I have seen people speaking highly about node http.request, and seeming to prefer it to making an AJAX call–but I don’t understand why. Is there a reason the http.request module is better to use than AJAX to make POST / GET calls?

(In my Electron app I already plan to use jQuery for other reasons, so will have access to the AJAX functionality from that.)

Thanks!


#5

I’m not sure, electron basically bundle node.js with chromium so that give you two way of doing some things.

If you think there’s any chance of creating a website version of the chat app, browser ajax could save you some work. If you plan to offer some feature without the need of a GUI, node would be ready for that.


#6

Just a follow-up on this for anyone interested. I have currently set up my app like mentioned above:
I have my electron app, which has front end stuff like the html views and front end javascript. Then I separately have an express/mongoDB setup like I would have in a normal web app. When the user takes certain actions in the front end electron app–such as hitting a “submit” button in an html view–my front-end javascript app sends a POST call to the Express “post” route, which then saves it to the MongoDB database.

In terms of making that POST call (or making GET, PUT, DELETE requests, etc. from Electron to the backend), I can do this with an AJAX call in the frontend javascript or using the http.request Node module. I have not determined which one is better–both seem to work.


#7

In this article https://dzone.com/articles/learning-electron-4-things-i-wish-i-knew-sooner it says it is best to stick to old-school web requests. Meaning use the frontend with XMLhttpRequest or Fetch.

Check out the article, it’s a good read in my opinion