Utilize Electron to Connect with the Backend

Currently, I am working on developing a small desktop application utilizing electron and P5 for the front-end. My goal is to make sure that this application operates seamlessly offline by storing data locally instead of relying on a database. The challenge I am facing is the inability to alter JSON files on the frontend, which means I need to establish a way to call functions from the backend (main file of the application) using the frontend. Since my experience with electron is limited, I have been struggling to find a solution online regarding how to make a button trigger a function in the main file. Any guidance or advice on this matter would be greatly appreciated as my attempts to resolve this issue independently have proven unsuccessful.

Answer №1

To make use of the built-in node process in Electron, you must first enable nodeIntegration.

To do this in your main.js file, ensure to enable nodeIntegration:

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        // ...
        webPreferences: {
            nodeIntegration: true,
        }
        // ...
    });
});

Once nodeIntegration is enabled, you can then freely utilize the electron remote module for tasks like performing file operations using the fs module:

const { remote } = require('electron');
const fs = remote.require('fs');

fs.writeFile('ourDataStorage.txt', data, (err) => {
   if (err) throw err;
   console.log('File has been saved.');
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Node.js Express server fails to retrieve data after a certain period of time

There is a problem with one specific route on the server, as it is returning a 404 error while all other routes are functioning properly. The route starts working and then suddenly stops without any clear reason. Here is the error message that I am receiv ...

What is the best way to effectively apply a mask within a PixiJS container so that its color does not display upon page refresh?

After implementing the code snippet below to add a mask in a container, I encountered an issue where upon clicking the refresh button on the page (chrome), the pixi stage would turn completely white until the refreshing process is completed. Can anyone p ...

Ajax continues to repeatedly redirect to the PHP page

I currently have a Shopify store and I am looking to incorporate a form on the product page. However, I am facing an issue where when I try to submit the form using AJAX with an external link, it redirects to the external page instead of staying on the cur ...

Extract JSON data from a JSON file referenced in another JSON file using C# programming language

Here I have two related classes structured as follows: public class school { public string Name{get;set} public int Identifier{get;set;} public field MyField{get;set;} } public class field { ...

Discovering which particular check in the Express Validator is causing the errors can be done by following these steps

I am currently developing a web application that requires an admin user to create new users. The admin user's username and password are stored in the .env file, and I am utilizing the dotenv package for this purpose. However, I am facing an issue when ...

Converting a JSON array into a Java HashMap with Jackson: A step-by-step guide

I would like to use Jackson to convert the JSON array below into a Java HashMap, and then iterate over the values as shown: Desired output format: key Value 1 sql 2 android 3 mvc JSON Sample: enter code here { "menu": [ { ...

Implementing USB trigger for cash drawer in web development

I am wondering about how to use a USB trigger to open a cash drawer (BT-100U). Can someone provide guidance on integrating this into a website? Here is a brief description of the BT-100U Cash Drawer Driver Trigger with USB Interface: This device allows fo ...

Utilizing JavaScript in Selenium WebDriver, what's the procedure for extracting element information from a script tag?

I'm facing an issue with this particular page There is a canvas element on this page that displays changing numbers whenever the page is refreshed or reloaded. Looking at the source code, I can only find the following: <div class="row"> ...

Troubleshooting problem with resizing and links in IE11 for iframe-resizer

When using the iframe-resizer from https://github.com/davidjbradshaw/iframe-resizer, I have encountered a few issues. Upon resizing the browser window, I noticed that if I first restore down and then manually resize, some extra padding appears. The paddin ...

How to Perform a Successful MongoDB Query without Encountering the TypeError: Converting Circular Structure to JSON

I am facing an issue with a query that I have executed multiple times. Please take a look at the code as I am unable to comprehend the problem. /* GET all things */ app.get('/things', function(req, res) { var search = req.query.search; c ...

Instructions on how to dynamically show specific text within a reusable component by utilizing React and JavaScript

My goal is to conditionally display text in a reusable component using React and JavaScript. I have a Bar component that I use in multiple other components. In one particular ParentComponent, the requirement is to show limit/total instead of percentage va ...

The number range filter in ng-table is malfunctioning

what I am trying to accomplish My goal is to create a column that can accommodate two numbers in order to filter numeric data within a specific range for that column. While sorting, pagination, and filtering by 'contain text' are working correct ...

Tomcat running on AWS ElasticBeanstalk does not support JSON input

I developed a spring MVC application using the spring tools for eclipse and deployed it on a tomcat 7 environment on AWS Elastic Beanstalk. However, I encountered an error when trying to return JSON. 406- The resource identified by this request is only c ...

Sorting Json Data Using Vue

My experience with Vue.js led me to a challenge I can't quite figure out: how to filter specific data in a Json file. Let me provide more context: I have a Json file filled with various electronics products such as computers, mice, keyboards, etc. I w ...

The behavior of Ajax is resembling that of a GET method, even though its intended method

Greetings, I am currently facing an issue while coding in Javascript and PHP without using jQuery for Ajax. My aim is to upload a file over Ajax and process it in PHP. Here is the code snippet: index.html <html> <head> <title>PHP AJAX ...

Is there a way to extract individual values from a for-each loop in JavaScript?

Would appreciate any guidance on my use of Bootstrap vue table with contentful's API. I'm currently working on implementing a for loop to iterate through an array and retrieve the property values. Although the console.info(episodes); call success ...

Troubleshooting issues with JavaScript progress bar functionality

I have implemented a progress bar using HTML5, JavaScript and Ajax to showcase file uploads in PHP. The issue I am facing is that the progress bar is not displaying the progress correctly. In addition to that, the echo statements in the PHP code are no ...

An issue arises when ReactRouter's useLocation is utilized, causing React to throw

https://i.sstatic.net/MtoLc.png Encountering an issue with useContext while utilizing the React Router useLocation hook. Attempting to retrieve information about the current page location using useLocation, but receiving the following error: useContext ...

Each time input is entered, the JSON data is duplicated and the entered input is not visible on the screen

As I delve into learning Next.js, my goal is to create a basic todo list using a JSON todos API for data. In the process of building the program, I opted to utilize Redux Toolkit for fetching the todos data. However, I am encountering an issue where newly ...

Black-colored backdrop for Mui modal

Currently working with a mui modal and encountering an issue where the backdrop appears as solid black, despite setting it to be transparent. I attempted to adjust the color to transparent, but the issue persists. ...