電子はAngularJsアプリを実行しても問題ありません。 私は、Angularを使用してCSRFの問題がなく、いくつかのElectronアプリを構築しました。
これを回避する1つの方法は、以下に示すように、電子内で実行されている単純なサーバーを作成することです:
// <YOUR-ENTRY-FILE>.js
app.on('ready', function() {
mainWindow = new BrowserWindow({
width: 800,
height: 600
});
var server = http.createServer(requestHandler).listen(9527);
mainWindow.loadUrl('http://localhost:9527/index.html');
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.setTitle(app.getName());
});
mainWindow.on('closed', function() {
mainWindow = null;
server.close();
});
});
function requestHandler(req, res) {
var
file = req.url == '/' ? '/index.html' : req.url,
root = __dirname + '/www',
page404 = root + '/404.html';
getFile((root + file), res, page404);
};
function getFile(filePath, res, page404) {
fs.exists(filePath, function(exists) {
if(exists) {
fs.readFile(filePath, function(err, contents) {
if(!err) {
res.end(contents);
} else {
console.dir(err);
}
});
} else {
fs.readFile(page404, function(err, contents) {
if(!err) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(contents);
} else {
console.dir(err);
}
});
}
});
};
本当にそこに、あなたがこれを行うために必要とされ、あなたのパスをチェックして、これを持つべきではありません最後のオプションとして。
出典
2016-05-16 19:24:08
Adi
コメントありがとうございました。私はこれが本当に少し "汚い"と思うが、httpサーバとしてアプリケーションを実行し、電子でnodejsを使うという選択肢を持つことはいいことだ。コード例をありがとう、それはトリックを行います。 –