This month I started learning electronjs, and although I all ready have some prototypes together for actual projects, I still think I have a lot to learn about the various modules in electron. So for todays post on electron I thought I would create a quick example centered around the app module of electron, and the various features that I should be aware of in that specific module. The main thing that the app module is used for would be to attach event handers for a wide range of events that happen when an application starts, is used, and closed. For example there is attaching an event handler for the ready event that should fire once Electron has finished initializing. Sense there is a ready event it goes without saying that there is also will-quit, and quit events that can be used to define some logic that should fire when an application is being closed.
This app module is one of the many main process modules to work with that should not be confused with other modules that are used for a preload script, or render process as it often seems to be called.
In this post then I will be going over the source code of an electronjs example that just makes use of a few features of the app module in electronjs. This will not be a comprehensive example of course, as there are a whole lot of features for this one that will have to be coved in any and all future posts on electronjs. However when it comes to learning electron, or anything really, one must start somewhere, and this will then be a kind of hello world type example but with the app module specify. While doing so I will need to also write about at least a few other modules in electronjs such as the BrowserWindow module, ipcMain, and the ipcRender module sense I will want to have a front end for this example.
When making an electron.js project one of the first and for most files that I am going to need is a main.js file at the root of the project folder. It is in this file that I will be requiring in the app module along with BrowserWindow, and ipcMain. The browserWidnow Module is what I am going to be using to create a window in which to have a front end for this example, and the ipcMain module is what I am going to use to define at least one event that I will be using with a preload script that will be used to create a custom api for this front end.
For this example I am using the app.whenReady method to define what should happen when the application fir starts. For this example I just want to create the main browser window instance in the ready event, and inside the function that is used to do this I am using also use the app.getPath method. This get path method is then yet another app module feature to get what the path is to the documents folder for the current user in the os as well as a lot of other paths like that depending on the string value given as the first argument when calling it.
I am also using the app on method to attach an event hander for the ‘window-all-closed’ event, and if the platform is not darwin by checking the process global in nodejs, I am calling the app quit method.
There is a great deal more to write about with the app module, and a whole world more to write about when it comes to electron as a whole as well of course. However what I wanted to do is just have a kind of central post on this module, not a post on just one specific feature of this module. I have a lot of topics that I like to write about on this site, but if I do work with electron more I am sure I will be writing more posts on this app module a well as the many other electronjs, nodejs, and client side features there are to work with when it comes to making some kind of desktop application this way.