在没有 GUI 的情况下运行 Electron [英] Run Electron without GUI

查看:32
本文介绍了在没有 GUI 的情况下运行 Electron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题已被问到,但该问题的上下文不同,因此有没有与我的用例相关的答案,所以我要问一个新问题

I see this question has been asked but the context of that question is different and so there are no answers relavent to my use case so I'm asking a new question

我有一个 基于电子的 Web 服务器.它适用于不习惯命令行的初级 Web 开发人员、学生和艺术家(是的,这是一场激烈的辩论,但我被 UCLA 艺术与设计学院的老师责骂让他们的学生使用命令行,因此是一个带有 GUI 的简单服务器.请不要争论这一点.这不是问题的重点)

I have an Electron based Web Server. It's meant for beginning web developers, students, and artists that are not used to the command line (yes that's a huge debate right there but I was scolded by the teachers at the Art and Design School at UCLA for asking their students to use the command line, hence a simple server with a GUI. Please don't debate this point. It's not the point of the question)

无论如何,使用此 Web 服务器的人有时希望在端口 80 上运行.在 Mac(和 Linux?)上,端口 80 需要管理员权限,因此我想升级服务器的权限来执行此操作.

In any case, people using this web server sometimes want to run on port 80. On Mac (and Linux?) port 80 requires admin rights and so I'd like to escalate the server's permissions to do this.

我的理解是最简单(唯一?)的方法是生成另一个进程.由于我的 Web 服务器的服务器部分实际上是用 node.js 编写的,并且由于 node.js(或至少是 API)内置于 Electron 中,所以似乎最好的解决方案是重新生成我的 Electron 应用程序以运行只有服务器部分.

My understanding is the easiest (only?) way to do this is to spawn another process. Since the server part of my web server is effectively written in node.js and since node.js (or at least the API) is built into Electron then it seems like the best solution would be to just re-spawn my Electron app to run only the server portion.

换句话说,想象一下我可以像这样运行应用程序

In other words, imagine I could run the app like

/Applications/Servez.app/Contents/MacOS/Servez --no-gui --server-only --port 80

然后我可以在 Mac 上使用一些 spawn 命令,比如

Then I could use some spawn command on Mac like

/usr/bin/osascript -e '
do shell "/Applications/Servez.app/Contents/MacOS/Servez --no-gui --server-only --port 80" with administrator privileges
'

macOS 会在运行服务器之前要求管理员权限.

And macOS will ask for admin privileges before running the server.

如果我不能在没有 GUI 的情况下运行 Electron,那么我可以在 Electron 包中包含一个 node.js 的副本,但考虑到相同的功能已经在 Electron 中,这似乎是一种巨大的空间浪费.或者也许还有其他解决方案?(我可以进行一些 API 调用来请求使用端口 80 的权限?)

If I can't run Electron with no GUI then I could include a copy of node.js inside the Electron package but it seems like a huge waste of space given the same functionality is already inside Electron. Or maybe there is some other solution? (some API call I can make to ask for permission to use port 80?)

推荐答案

设置环境变量 ELECTRON_RUN_AS_NODE 将 Electron 作为节点运行,所以我可以在生成 Electron 时设置该环境变量

Setting the environment variable ELECTRON_RUN_AS_NODE will run Electron as node so I can just set that environment variable when spawning Electron

这篇关于在没有 GUI 的情况下运行 Electron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆