简体   繁体   English

Nodejs运行js文件的过程

[英]The process of Nodejs running a js file

我是Nodejs世界的新手,我有一些问题。为什么当我使用命令node xxx.js时,节点服务不需要启动而apache服务需要启动,它可以给我结果,节点如何做到这一点?此过程的步骤是什么。

node is a program that contains the Javascript interpreter. node是一个包含Javascript解释器的程序。 To run a script file, you run node and pass it the script you want to run on command line. 要运行脚本文件,请运行节点并向其传递要在命令行上运行的脚本。 It initializes itself, then loads and runs the desired script. 它会初始化自身,然后加载并运行所需的脚本。

node is more generic than Apache. 节点比Apache更通用。 It is not, by itself, a server of any kind. 它本身不是任何类型的服务器。 If you want a node.js app to be a server, you have to start a server yourself in your Javascript code. 如果希望将node.js应用程序用作服务器,则必须使用Javascript代码自己启动服务器。

Apache is a web server. Apache是​​Web服务器。 When you start it, it starts a web server and then you can run things in the context of that web server. 启动它时,它将启动Web服务器,然后您可以在该Web服务器的上下文中运行事物。 Apache does not contain it's own language interpreter like node does. Apache不像node那样包含它自己的语言解释器。 It can run other types of code like PHP, but you have to supply it with a PHP interpreter in order for it to do that. 它可以运行其他类型的代码,例如PHP,但是您必须为其提供PHP解释器才能做到这一点。 node, on the other hand, has a Javascript interpreter built in. 另一方面,节点内置了Java解释器。

While node can be used to create a web server by writing a Javascript script that creates and starts a web server, node is more generic than Apache. 虽然可以通过编写可创建和启动Web服务器的Javascript脚本来使用node创建Web服务器,但node比Apache更通用。 node can be used for all sorts of non-web server types of things. 节点可用于各种非Web服务器类型的事物。 For example, I have a nodejs script on my computer that runs automatically each night that cleans up a bunch of automatic backup files on my disk by aging them (files older than a week are deleted). 例如,我的计算机上有一个nodejs脚本,该脚本每天晚上自动运行,通过老化它们来清除磁盘上的一堆自动备份文件(删除了一周以上的文件)。 That isn't something you would do with Apache. 那不是用Apache要做的事情。

So, while there's some functionality overlap only because you can create a web server with node.js, node and Apache are fundamentally different types of tools. 因此,虽然有些功能重叠只是因为您可以使用node.js创建Web服务器,但是node和Apache根本是不同类型的工具。

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

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