James Shaji James Shaji - 4 years ago 170
Node.js Question

Electron close button not working

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});

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.on('closed', function() {
mainWindow = null;

app.on('window-all-closed', function() {
if (process.platform != 'darwin') {

exapp.get('/editPage', function(req,res){
//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){
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}));
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) {
console.log("End of write file");
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;
}, function error(response) {
console.log("HTTP Error " + response.statusText);

Commenting file write code in '/saveContent' doesn't cause electron close button to become unresponsive.

Answer Source

I replace the code for mainWindow close to as below and works fine

    mainWindow.on('close', function(e) { 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download