Electronic processing input

I recently started peeing with Electron. I really like the principles behind it, but I find it a little confusing to do something.

For example, how do you handle user input? I have main.js and BrowserWindow pointing to a local html file (containing some user settings with an input field).

How do I access this data when submitting an HTML form (to the same file or another)?

main.js

const {app, BrowserWindow} = require('electron') let win function createWindow () { win = new BrowserWindow({width: 800, height: 600}) win.loadURL('file://' + __dirname + '/index.html') // Emitted when the window is closed. win.on('closed', () => { win = null }) // Open the DevTools. // win.webContents.openDevTools() } app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { if (win === null) { createWindow() } }) // In this file you can include the rest of your app specific main process // code. You can also put them in separate files and require them here. //Start the main window app.on('ready', createWindow) 

index.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="" method="post"> <input type="text" name="test-1"> </form> </body> </html> 
+6
source share
1 answer

With Electron, node.js does not act like a web server with routes such as in a typical web application scenario. Instead of sending requests for routes, you will create a one-page application using a javascript framework such as Angular, React, Knockout, etc. At this point, you no longer need to handle routing. You bind your "Send" event to the client to the javascript function directly on the page and process the input from it.

You can do everything from the javascript context of the page, which you can do from the context of the main node.js process. For example, if you need to access the file system from your page, you must use the Remote module to access node.js.

For instance:

 // Gain access to the node.js file system api function useNodeApi() { const remote = require('electron').remote; const fs = remote.require('fs'); fs.writeFile('test.txt', 'Hello, I was written by the renderer process!'); } 

I rarely came across a situation where I needed to go to the main process in order to accomplish something. After starting BrowserWindow, everything you ever need to do can be done from the rendering process. This pretty much eliminates the need to do things like send form messages via http.

+4
source

Source: https://habr.com/ru/post/1011817/


All Articles