I am trying to create an application using Electron (formerly Atom Shell). This application wraps an AngularJS application and interacts with endpoints created in nodejs to edit and save the HTML content. I am able to create the application with no issues.
When I try to access "/saveContent" from electron causes close button (Windows close on top right corner) to become unresponsive, however minimize and maximize works fine without issue. If I access any other endpoint through electron this issue doesn't come up. I have tried with both sync file write and otherwise too. So I assume "/saveContent" in main.js is cause of the issue.
If I end node.exe in "Windows task Manager" this closes the whole application.
I have the main process code below
'use strict';
var fs = require("fs");
const util = require('util')
var cheerio = require("cheerio");
var express = require('express');
var exapp = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended: false});
exapp.use(bodyParser.json());
const electron = require('electron');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const path = require('path')
const url = require('url')
var mainWindow = null;
app.on('ready', function() {
mainWindow = new BrowserWindow({width: 1280, height: 900, title: "2018JL", "nodeIntegration":false});
//mainWindow.loadURL(__dirname + '/app/index.html');
mainWindow.loadURL('http://localhost:5001/');
mainWindow.on('closed', function() {
mainWindow = null;
});
});
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
exapp.get('/editPage', function(req,res){
if(req){
//console.log("req.query.editURL "+ req.query.editURL);
var url = req.query.editURL;
var editURL = path.join(__dirname + '/app/views'+ url+".html");
fs.exists(editURL, function(fileok){
if(fileok){
fs.readFile(editURL, 'utf8', function (err, data) {
if (err) {
console.log("error.... "+ err);
return console.error(err);
}
//console.log("data "+ editURL);
res.send(JSON.stringify({path:editURL, content:data}));
});
}else{
console.log("file not found");
}
});
}
});
exapp.post('/saveContent', function (req, res) {
//console.log(util.inspect(req, false, null))
if (req) {
//console.log(req.query.url + " ------ " + req.query.content);
var $ = cheerio.load(req.query.content);
var htmlContent = $('body').children();
console.log('htmlContent '+htmlContent);
fs.writeFile(req.query.url, htmlContent, function(err) {
if (err) {
res.send("Error");
}
console.log("End of write file");
res.send("success");
});
}
console.log("End of function .....");
});
exapp.get('/test', function (req, res) {
res.send("Test success ");
});
exapp.use(express.static(__dirname + '/app'));
exapp.listen(process.env.PORT || 5001);
Client code below
$scope.editPage = function () {
$http({method: "GET",
url: "/editPage",
params: {editURL: $location.path()}
}).then(function success(response) {
//var result = JSON.parse(response.data);
//console.log("HTTP Success "+response.data.path);
$scope.showEditor = true;
$scope.editURL = response.data.path;
tinymce.get('contentEditor').setContent(response.data.content);
}, function error(response) {
console.log("HTTP Error " + response.statusText);
});
};
Commenting file write code in '/saveContent' doesn't cause electron close button to become unresponsive.
I replace the code for mainWindow close to as below and works fine
mainWindow.on('close', function(e) {
e.preventDefault();
mainWindow.destroy();
});
https://github.com/electron/electron/blob/master/docs/api/browser-window.md#windestroy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With