最近我开始开发我的第一个电子应用程序,遇到了一个问题,希望有人能帮我解决这个问题。
我想使用电子快速创建一个很酷的应用程序,使用一个管理后端的AngluarJS HTML主题。
AngluarJS主题在http://源代码中运行良好,但当我从本地驱动器(如C:\驱动器)加载主题时,它会破坏主题,因为(我认为)在chrome中使用了CSRF保护策略。
现在我谷歌了我的屁股,但我找不到一个好的解决方案。我认为以电子方式运行http-服务器可能是一种解决方案,所以我尝试了npm http-server包。这种方法的问题是nodejs代码在这种情况下不再工作了,因为http-服务器只会处理静态文件。
还有别的解决办法吗?
谢谢你和我一起思考!
发布于 2016-05-16 19:24:08
电子运行AngularJs应用程序没有问题。我已经建立了一些电子应用程序使用角没有CSRF问题。
解决这个问题的一种方法是创建一个运行在电子内部的简单服务器,如下所示:
// <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);
}
});
}
});
};你真的不应该需要这样做,检查你的路径,并把这作为最后的选择。
发布于 2020-02-28 13:37:00
很晚了,但我找到了解决这个问题的办法,也许有一天它能帮到某人https://stackoverflow.com/a/60452312/1284216
https://stackoverflow.com/questions/37026659
复制相似问题