Javascript 基于 Node.js 的服务器与类似 Apache HTTP 服务器的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38821947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:53:46  来源:igfitidea点击:

Node.js based server vs something like the Apache HTTP server

javascriptnode.jsapachenginx

提问by Grateful

I have been studying Node.js recently and came across some material on writing simple Node.js based servers. For example...

我最近一直在研究 Node.js,并且遇到了一些关于编写基于 Node.js 的简单服务器的材料。例如...

var express = require("express"),
http = require("http"), app;

// Create our Express-powered HTTP server
// and have it listen on port 3000
app = express();
http.createServer(app).listen(3000);

// set up our routes
app.get("/hello", function (req, res) {
    res.send("Hello World!");
});

app.get("/goodbye", function (req, res) {
    res.send("Goodbye World!");
});

... Now, although I seem to understand what's going on in the code... I am slightly confused by the terminology.... because when I hear the term server, I think about stuff like Apache or Nginx. I am used to thinking of them being like a container that can hold my web applications. How does Node.js server differ from Nginx/Apache server? Isn't it true that a Node.js based server (i.e. code) can still be placed within something like Nginx to run? So why are both called "servers", although the Node.js code seems to be the application that can be placed and served using Nginx.

……现在,虽然我似乎明白代码中发生了什么……我对术语有点困惑……因为当我听到术语服务器时,我会想到像 Apache 或 Nginx 这样的东西。我习惯于认为它们就像一个容器,可以容纳我的 Web 应用程序。Node.js 服务器与 Nginx/Apache 服务器有何不同?难道基于 Node.js 的服务器(即代码)仍然可以放在 Nginx 之类的东西中运行吗?那么为什么两者都被称为“服务器”,尽管 Node.js 代码似乎是可以使用 Nginx 放置和服务的应用程序。

回答by slebetman

It's a server, yes.

这是一个服务器,是的。

A node.js web application is a full-fledged web server just like Nginx or Apache.

node.js Web 应用程序是一个成熟的 Web 服务器,就像 Nginx 或 Apache 一样。

You can indeed serve your node.js application without using any other web server. Just change your code to:

您确实可以在不使用任何其他 Web 服务器的情况下为您的 node.js 应用程序提供服务。只需将您的代码更改为:

app = express();
http.createServer(app).listen(80); // serve HTTP directly

Indeed, some projects use node.js as the front-endload balancer for other servers (including Apache).

事实上,一些项目使用 node.js 作为其他服务器(包括 Apache)的前端负载均衡器。

Note that node.js is not the only development stack to do this. Web development frameworks in Go, Java and Swift also do this.

请注意,node.js 并不是执行此操作的唯一开发堆栈。Go、Java 和 Swift 中的 Web 开发框架也这样做。

Why?

为什么?

In the beginning was the CGI. CGI was fine and worked OK. Apache would get a request, find that the url needs to execute a CGI app, execute that CGI app and pass data as environment variables, read the stdout and serve the data back to the browser.

一开始是CGI。CGI 很好,工作正常。Apache 会收到一个请求,发现该 url 需要执行一个 CGI 应用程序,执行该 CGI 应用程序并将数据作为环境变量传递,读取标准输出并将数据提供回浏览器。

The problem is that it is slow. It's OK when the CGI app was a small statically compiled C program but a group of small statically compiled C programs became hard to maintain. So people started writing in scripting languages. Then that became hard to maintain and people started developing object oriented MVC frameworks. Now we started having trouble - EVERY REQUEST must compile all those classes and create all those objects just to serve some HTML, even if there's nothing dynamic to serve (because the framework needs to figure out that there's nothing dynamic to serve).

问题是它很慢。当 CGI 应用程序是一个小的静态编译的 C 程序时是可以的,但是一组小的静态编译的 C 程序变得难以维护。于是人们开始用脚本语言写作。然后这变得难以维护,人们开始开发面向对象的 MVC 框架。现在我们开始遇到麻烦 - 每个请求都必须编译所有这些类并创建所有这些对象来提供一些 HTML,即使没有任何动态可以提供(因为框架需要弄清楚没有任何动态可以提供)。

What if we don't need to create all those objects every request?

如果我们不需要在每个请求中创建所有这些对象怎么办?

That was what people thought. And from trying to solve that problem came several strategies. One of the earliest was to embed interpreters directly in web servers like mod_phpin Apache. Compiled classes and objects can be stored in global variables and therefore cached. Another strategy was to do pre-compilation. And yet another strategy was to run the application as a regular server process and talk with the web server using a custom protocol like FastCGI.

人们是这么想的。从试图解决这个问题中产生了几种策略。最早的方法之一是将解释器直接嵌入到 Web 服务器中,例如mod_php在 Apache 中。编译后的类和对象可以存储在全局变量中,因此可以缓存。另一种策略是进行预编译。另一个策略是将应用程序作为常规服务器进程运行,并使用 FastCGI 等自定义协议与 Web 服务器通信。

Then some developers started simply using HTTP as their app->server protocol. In effect, the app is also an HTTP server. The advantage of this is that you don't need to implement any new, possibly buggy, possibly not tested protocol and you can debug your app directly using a web browser (or also commonly, curl). And you don't need a modified web server to support your app, just any web server that can do reverse proxying or redirects.

然后一些开发人员开始简单地使用 HTTP 作为他们的应用程序->服务器协议。实际上,该应用程序也是一个 HTTP 服务器。这样做的好处是您不需要实现任何新的、可能有问题、可能未经测试的协议,并且您可以直接使用 Web 浏览器(或通常使用 )调试您的应用程序curl。而且您不需要修改的 Web 服务器来支持您的应用程序,只需任何可以执行反向代理或重定向的 Web 服务器。

Why use Apache/Nginx?

为什么使用 Apache/Nginx?

When you serve a node.js app note that you are the author of your own web server. Any potential bug in your app is a directly exploitable bug on the internet. Some people are (justifiably) not comfortable with this.

当您提供 node.js 应用程序时,请注意您是自己的 Web 服务器的作者。您的应用程序中的任何潜在错误都是互联网上可直接利用的错误。有些人(有理由)对此感到不舒服。

Adding a layer of Apache or Nginx in front of your node.js app means you have a battle-tested, security-hardened piece of software on the live internet as an interface to your app. It adds a tiny bit of latency (the reverse proxying) but most consider it worth it.

在您的 node.js 应用程序前面添加一层 Apache 或 Nginx 意味着您在实时互联网上拥有一个经过实战测试、经过安全加固的软件,作为您的应用程序的接口。它增加了一点延迟(反向代理),但大多数人认为这是值得的。

This used to be the standard advice in the early days of node.js. But these days there are also sites and web services that exposes node.js directly to the internet. The http.Servermodule is now fairly well battle-tested on the internet to be trusted.

这曾经是 node.js 早期的标准建议。但是现在也有一些站点和 Web 服务将 node.js 直接暴露给 Internet。该http.Server模块现在在互联网上经过了相当好的实战测试,值得信赖。

回答by Naeem Shaikh

NodeJs creates its own server. As you can see, terminology is quite clear:

NodeJs 创建自己的服务器。如您所见,术语非常清楚:

http.createServer(app).listen(3000);

Create a server and listen for http requests on port 3000.

创建一个服务器并在端口 3000 上侦听 http 请求。

We used nginx in one of our project, but it was more like a loadbalancer for multiple nodejs instances.

我们在我们的一个项目中使用了 nginx,但它更像是多个 nodejs 实例的负载均衡器。

Lets say you have two nodejs instances running on port 3000 and 3001, Now you can still use nginxas a server to listen your actual httpcalls on port 80, and may want to redirect your request to nodejsserver or maybe some other server, more like a loadbalancer. So you can still use whatever nginxprovides with nodejs.

比方说你有3000端口和3001运行两个实例的NodeJS,现在你仍然可以使用nginx一个服务器监听你的实际http的来电port 80,并可能要重定向您的要求nodejs服务器或者一些其他的服务器,更像是一个loadbalancer。所以,你仍然可以使用任何nginx与提供nodejs

A good question already asked here.

这里已经问一个好问题。

回答by Vamshi Krishna

Assume there is a hotel named Apache Hotel which has a waiter for each customer.

假设有一家名为 Apache Hotel 的酒店,它为每位顾客配备了一名服务员。

As soon as the customer orders a salad, the waiter goes to the chef and tells him. While the chef prepares the food, the waiter waits. Here,

顾客一点了沙拉,服务员就会去找厨师告诉他。当厨师准备食物时,服务员在等待。这里,

Chef => File System,

Chef => File System,

Waiter => Thread,

Waiter => Thread,

Customer => Event.

Customer => Event.

Even when the customer orders water the waiter brings only after serving the salad. The waiter keeps on waiting until the salad is prepared by the chef. This state is referred as blocking state. Even if the hotel grows each customer should have different waiters to serve. This increases the blocking of threads(waiters).

即使顾客点了水,服务员也是在沙拉上完之后才拿来的。服务员一直在等待,直到厨师准备好沙拉。这种状态称为阻塞状态。即使酒店发展壮大,每个客户也应该有不同的服务员来服务。这增加了线程(服务员)的阻塞。

Now, coming to Node Hotel there is only one waiter for all the customers. If first customer orders soup the waiter tells the chef and goes to second customer. After the food is ready the waiter delivers to the customer. Here the customer will not wait. This state is referred as Non-Blocking state. The single waiter(Thread) servers all the customer and makes them happy.

现在,来到节点酒店,所有客人都只有一个服务员。如果第一个顾客点了汤,服务员会告诉厨师然后去找第二个顾客。食物准备好后,服务员将食物送到顾客手中。在这里,客户不会等待。这种状态称为非阻塞状态。单个服务员(线程)为所有客户提供服务并让他们开心。

Thus, Node which is a single threaded application is very fast.

因此,作为单线程应用程序的 Node 速度非常快。