使用Node.js和Express,如何在后端隐藏JavaScript代码?

huangapple go评论71阅读模式
英文:

Using Node.js and Express, how to hide Javascript code in backend?

问题

I understand your request. Here's the translated part:

我是新手使用Node和Express。我想问一个关于如何隐藏后端JavaScript代码的问题。我正在使用Node.js和Express。我想避免通过浏览器检查来读取它。我创建了一个简单的脚本,并将链接放在index.html中的head内,写入<script src="/javascripts/test.js"></script>。一切都正常,正常工作。

我尝试检查页面,使用Google,通过打开<script src="/javascripts/test.js"></script>可以轻松读取代码。

我认为原因是因为我在浏览器前端中使用了test.js。如何在后端中使用它并避免其他人阅读代码?如何做到这一点?你可以解释一下吗?谢谢。

供信息和完整性,脚本如下:

function myFunction() {
    alert("I am an alert box!");
}

而在HTML按钮上,我写了:

<button onclick="myFunction()" type="button" class="btn btn-primary">Primary</button>
英文:

I'm new with Node and Express. I would like to ask a question about how to hide Javascript code in backend. I'm using Node.js with Express. I want to avoid reading with browser inspection. I created a simple script and put the link in index.html, inside head, writing &lt;script src =&quot;/javascripts/test.js&quot;&gt;&lt;/script&gt;. Everything ok, works normally.

I tried to inspect the page, with Google, and the code is easily read by opening <script src ="/javascripts/test.js"></script>.

I think the reason is because i use test.js in the browser front-end side. How can I use it in the backend and avoid that someone can read the code? How can this be done and how? Can you explain me? Thank you

For information and completeness, the script is

 function myFunction() {
    alert(&quot;I am an alert box!&quot;);
  }

While on the html button I wrote:

&lt;button onclick=&quot;myFunction()&quot; type=&quot;button&quot; class=&quot;btn btn-primary&quot;&gt;Primary&lt;/button&gt;

答案1

得分: 3

"alert"函数,定义在HTML规范中,会导致浏览器使用浏览器的本机用户界面显示模态消息框。它是供客户端JavaScript使用的API。

虽然Node.js允许您在服务器上执行JavaScript,但它只允许您使用JavaScript语言功能Node.JS API(以及受到相同限制的第三方代码)。

您不能在Node.js中使用Web API或其他特定于浏览器的功能(除非它们具有明确的Node.js等效功能),因为服务器无法访问它们。

在浏览器中显示模态消息框是一种固有的在浏览器中完成的操作,因此没有等效的Node.JS API。

英文:

The alert function, defined in the HTML specification, causes the browser to display a modal message box using the browser's native UI. It's an API made available to client-side JavaScript.

While Node.js allows you to execute JavaScript on the server, it only allows you to use JavaScript language features and Node.JS API (along with third-party code subject to the same limitations).

You can't use Web APIs or other browser-specific features in Node.js (excepting those which have explicit Node.js equivalents), because the server doesn't have access to them.

Making a modal message box appear in the browser is inherently something that has to be done in the browser, so there can't be an equivalent Node.JS API.

huangapple
  • 本文由 发表于 2023年6月12日 22:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定