在Electron中,我试图获取通常具有
file://
网址来源
取而代之的是
本地主机。
为此,我在电子应用程序中设置了一个内部http服务器来处理服务页面(完整代码为下面的 quark.js )。
server = spawn("node", [path.join(opts.dir, "../../../app.asar.unpacked/http/page-server.js")], {
env: {
QUARK_PORT: opts.port || 21048,
QUARK_DIR: opts.dir || __dirname,
QUARK_DESC: opts.description || "default",
QUARK_LOG: opts.log || false
}});
问题-以上内容适用于装有Node的计算机,但NodeJS在生产计算机上不可用。
问题-此you can fork nodejs file inside electron process建议您无需在生产计算机上使用NodeJS即可完成此操作。它是否正确?当我尝试遵循而不是生成时,似乎无法使用错误no 'on' method of undefined
来解决此问题。
const { fork } = require("child_process");
server = fork(
path.join(opts.dir, "../../../app.asar.unpacked/http/page-server.js"),
[],
{
env: {
QUARK_PORT: opts.port || 21048,
QUARK_DIR: opts.dir || __dirname,
QUARK_DESC: opts.description || "default",
QUARK_LOG: opts.log || false
}
}
);
摘要
main.ts(电子)
import { app, BrowserWindow } from "electron";
import * as path from "path";
const os = require("os");
const quark = require("../../http/quark");
quark({
port: 3000,
dir: path.join(__dirname, "../../../app.asar.unpacked/dist/procurement-app"),
description: "HTTP Server"
});
let win: BrowserWindow;
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
win.loadURL(quark.url(`index.html`));
win.on("closed", () => {
win = null;
});
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (win === null) {
createWindow();
}
});
quark.js
/**
* To get Auth0 to work by using an internal express server in my electron app to handle serving pages.
* Otherwise the lock fails with a 403 error and responds with "Origin file:// is not allowed".
*
* https://stackoverflow.com/questions/48465130/electron-auth0lock-origin-file-not-allowed/48466642#48466642
* https://github.com/lawrencezahner/quark
*/
const { spawn } = require("child_process");
const path = require("path");
const url = require("url");
let server;
var quark = function(opts) {
if (server == null) {
server = spawn(
"node",
[path.join(opts.dir, "../../../app.asar.unpacked/http/page-server.js")],
{
env: {
QUARK_PORT: opts.port || 21048,
QUARK_DIR: opts.dir || __dirname,
QUARK_DESC: opts.description || "default",
QUARK_LOG: opts.log || false
}
}
).on("error", err => {
console.log("spawn error");
throw err;
});
server.opts = opts;
/** Logging */
server.stdout.on("data", data => {
console.log(data.toString());
});
server.stderr.on("data", data => {
console.log(data.toString());
});
}
};
quark.url = function(path) {
return url.format({
hostname: "localhost",
port: server.opts.port,
pathname: path,
protocol: "http"
});
};
quark.html = function(path) {
return quark.url(`${path}.html`);
};
quark.js = function(path) {
return quark.url(`${path}.js`);
};
quark.scss = function(path) {
return quark.url(`${path}.scss`);
};
module.exports = quark;
page-server.js
const http = require("http");
const path = require("path");
const url = require("url");
const fs = require("fs");
const mime = require("mime-types");
const server = http.createServer(function(req, res) {
/** Logging */
console.log(`${req.method} ${req.url}`);
var purl = url.parse(req.url);
var pathname = path.join(
process.env.QUARK_DIR,
"../../../app.asar.unpacked/dist/procurement-app",
purl.pathname
);
var ext = path.parse(pathname).ext;
fs.exists(pathname, exists => {
if (!exists) {
res.statusCode = 404;
res.end(`Could not find ${pathname} :(`);
return;
} else {
fs.readFile(pathname, (err, data) => {
if (err) {
res.statusCode = 500;
res.end(`Had trouble getting ${pathname}`);
} else {
res.setHeader("Content-type", mime.contentType(ext) || "text/plain");
res.setHeader(
"Access-Control-Allow-Origin",
url.format({
hostname: "localhost",
port: process.env.QUARK_PORT,
protocol: "http"
})
);
res.end(data);
return;
}
});
}
});
});
server.listen(process.env.QUARK_PORT);
/** Logging */
console.log(`Quark Server started on ${process.env.QUARK_PORT}`);
文件夹结构
├───app-builds
│ ├───.icon-ico
│ └───win-unpacked
│ ├───locales
│ ├───resources
│ │ └───app.asar.unpacked
│ │ ├───dist
│ │ │ └───procurement-app
│ │ ├───http
│ │ └───node_modules
│ │ ├───mime-db
│ │ └───mime-types
│ └───swiftshader
├───dist
│ └───procurement-app
│ └───assets
│ ├───icons
│ └───images
├───electron
│ └───[main.ts]
│ └───dist
│ └───[main.js]
├───http
│ └───[page-server.js, quark.js]
├───node_modules
└───src
└───app
请注意,我正在关注这个问题Electron Auth0Lock "Origin file:// not allowed"的答案(问题与我尝试这样做的原因相同)