简体   繁体   English

关于如何在服务器端和客户端执行 javascript 的困惑

[英]Confusion on how to execute javascript server-side vs client-side

I'm unsure if I'm asking the right question here.我不确定我在这里问的问题是否正确。 I apologize in advance for my ignorance, I'm only a year into teaching myself full stack JS and web app architecture and I'm trying to understand this on a granular level.我提前为我的无知道歉,我只花了一年的时间自学全栈 JS 和 Web 应用程序架构,我正试图在粒度层面上理解这一点。 Previous attempts to ask this question seem to offend some devs, so maybe it's a dumb question.以前提出这个问题的尝试似乎冒犯了一些开发人员,所以这可能是一个愚蠢的问题。

I'm trying to understand how to, or if its possible to, explicitly designate or assign a specific file or block of javascript to process/run/execute on a server vs on the client/browser.我试图了解如何,或者如果可能的话,明确指定或分配特定文件或 javascript 块以在服务器上与客户端/浏览器上进行处理/运行/执行。

Writing some javascript in a an app.js file and running node app.js seems to still expose that script to the browser, viewable in the Sources tab in Dev Tools.在 app.js 文件中编写一些 javascript 并运行node app.js似乎仍然将该脚本暴露给浏览器,可以在 Dev Tools 的 Sources 选项卡中查看。

PHP is interpreted by a server and then sends static HTML to the client. PHP 由服务器解释,然后将静态 HTML 发送到客户端。 Is this "pre-processing", the equivalent of Server Side Rendering with Javascript App frameworks?这是“预处理”,相当于使用 Javascript 应用程序框架进行服务器端渲染吗? Or Is writing a.php file inline with HTML server-side (like you'd see in Wordpress) more equivalent to inline javascript client side?或者是否编写与 HTML 服务器端内联的 .php 文件(就像您在 Wordpress 中看到的那样)更等同于内联 javascript 客户端? And there is a different method to have JS interpreted server-side?还有一种不同的方法可以让 JS 在服务器端进行解释?

I've been reading about GENERATE_SOURCEMAP, but that seems more like it's used to hide client-side JS modules.我一直在阅读有关 GENERATE_SOURCEMAP 的内容,但这似乎更像是用于隐藏客户端 JS 模块。

Other possible wordings of this question这个问题的其他可能的措辞

  • How to not expose server side Javascript to the client?如何不向客户端公开服务器端 Javascript?
  • How to run/execute Javascript on a server vs in browser?如何在服务器和浏览器中运行/执行 Javascript?

I am NOT ASKING for the definition of server-side vs client-side JS, or for suggestions of server side application frameworks.不是在询问服务器端与客户端 JS 的定义,或者服务器端应用程序框架的建议。

Again, I think i've confused myself or missing something very basic.再一次,我想我把自己弄糊涂了,或者遗漏了一些非常基本的东西。 Maybe the only answer is to segment your private web application from your client application and access via API.也许唯一的答案是将您的私有 Web 应用程序与您的客户端应用程序分开并通过 API 访问。 Thank you for any help.感谢您的任何帮助。

Summary: Your JavaScript executed by the node daemon on the server side is not visible on the client side, the only thing visible is the output of your JS.总结:你在服务器端由节点守护进程执行的 JavaScript 在客户端是不可见的,唯一可见的是你的 JS 的输出。

When you create a file with the .php extension you need an executable that executes this file, to return the result to the client to render it.当您创建一个带有.php扩展名的文件时,您需要一个执行该文件的可执行文件,以将结果返回给客户端以呈现它。

For example:例如:

  • create a php file index.php with the following syntax:使用以下语法创建一个 php 文件index.php
<?php
    echo '<script>console.log("HelloWorled")</script>'
?>

to run this file, you need the PHP daemon (the php interpreter) to run the file to get this output <script>console.log("HelloWorled")</script> next;要运行此文件,您需要 PHP 守护程序(php 解释器)来运行该文件以获取此输出<script>console.log("HelloWorled")</script>接下来; the output will be returned to the client to be execute on client browser;输出将返回给客户端,在客户端浏览器上执行;

In the case of PHP this operation is managed directly by the http server such as nginx via this configuration:在 PHP 的情况下,此操作由 http 服务器(例如 nginx)通过以下配置直接管理:

server{
   location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/run/php/php<version>-fpm.sock;
   }
}

which you will find inside the /etc/nginx/sites-available/default file你会在/etc/nginx/sites-available/default文件中找到它

This rule simply tells nginx to make all files with the .php extension go through the php proxy to be executed and then return only the result of the execution to the client.这个规则只是告诉nginx让所有扩展名为.php的文件都经过php代理执行,然后只将执行结果返回给客户端。

You have to imagine the exact same thing with nodejs.您必须想象与 nodejs 完全相同的事情。

I created an index.js file with the following syntax:我使用以下语法创建了一个index.js文件:

....
process.write( '<script>console.log("HelloWorled")</script>');
....

running the file with node index.js you will just get the output <script>console.log("HelloWorled")</script> which your server (eg expressJS) will send back to the client to run it client side to get los same result of the php code.使用node index.js运行文件,您将得到输出<script>console.log("HelloWorled")</script> ,您的服务器(例如 expressJS)将发送回客户端以在客户端运行它以获取 los php代码的相同结果。

NOTES:笔记:

  • YOU DON'T NEED TO EXPOSE YOUR JS CODE, YOU NEED ONLY A PROXY TO SERVER IT Like PHP WITH NGINX (read more about expressJS and the other nodejs servers frameworks).你不需要暴露你的 JS 代码,你只需要一个代理来服务它,就像 PHP 和 NGINX(阅读更多关于expressJS和其他 nodejs 服务器框架)。
  • IF you need to expose a client side scripts,images,styles, you need to specify it in your proxy configuration如果您需要公开客户端脚本、图像、样式,您需要在代理配置中指定它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM