2017-01-30 92 views
2

我正在嘗試使用Electron(原名Atom Shell)創建應用程序。此應用程序包裝AngularJS應用程序,並與在nodejs中創建的端點進行交互以編輯和保存HTML內容。我能夠創建沒有問題的應用程序。電子關閉按鈕不起作用

當我嘗試從電子訪問「/ saveContent」導致關閉按鈕(Windows在右上角關閉)變得無法響應時,但是最小化和最大化工作正常,沒有問題。如果我通過電子訪問任何其他端點,則不會出現此問題。我已經嘗試了同步文件寫入和其他。所以我假設main.js中的「/ saveContent」是問題的原因。

如果我在「Windows任務管理器」中結束node.exe,則會關閉整個應用程序。

我具有低於

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

客戶端代碼的主進程的代碼下面

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

註釋文件寫入代碼在「/ saveContent」不會引起電子關閉按鈕無響應。

回答