简体   繁体   English

在Node.js中,创建多个“服务器”对象但仅将一个对象绑定到端口是否正常?

[英]In Node.js, is it normal to create several “server” objects, but only bind one to a port?

I'm just about done reading "Node.js in Action", and I'm trying to put together the pieces of Node.js --> Connect --> Express. 我即将读完“ Node.js in Action”,并且试图将Node.js的各个部分放在一起-> Connect-> Express。 I have a question about the "servers" that we create in Node. 我对在Node中创建的“服务器”有疑问。

node_server = http.createServer();
connect_app = Connect();
express_app = Express();

In the code above, is it true that connect_app is basically a "subclass" of node_server ? 在上面的代码中, connect_app node_servernode_server的“子类”吗? (I know, this is JavaScript, so we don't really have subclassing, but I don't know what else to call it; extension?). (我知道,这是JavaScript,所以我们实际上没有子类化,但是我不知道该怎么称呼它,扩展名?)。 And likewise express_app is basically a "subclass" of connect_app ? 同样, express_app基本上是connect_app的“子类”? It's my understanding that all of these objects are servers which could be bound to a port and respond to requests, but that in practice we typically only bind ONE of them to a port and use it to proxy requests to other server objects. 据我了解,所有这些对象都是可以绑定到端口并响应请求的服务器,但实际上,我们通常只将其中一个绑定到端口,并使用它将请求代理到其他服务器对象。

Am I on the right track in learning this? 我是否在学习此课程的正确道路上?

First of all, shake off the idea that there are 3 running servers - because there's only one. 首先,摆脱掉有3台正在运行的服务器的想法-因为只有一台。

Express is a framework that relies on Connect, which is another framework/set of middlewares. Express是一个依赖于Connect的框架,Connect是另一个中间件框架/集。 Further, Connect relies on the NodeJS's API (HTTP module). 此外,Connect依赖于NodeJS的API(HTTP模块)。 Basically an abstraction, one on top of another. 基本上是一种抽象,一种在另一种之上。

An analogy is that Express is a car, Connect is like an engine, NodeJS is the engine parts. 打个比方,Express是一辆汽车,Connect就像是引擎,NodeJS是引擎零件。 You only have one running car (one server in your case), but multiple components powering it. 您只有一辆正在行驶的汽车(您的情况下是一台服务器),但是有多个组件为其提供动力。

@josh3736 Has commented a better explanation how it works. @ josh3736 评论了一个更好的解释它是如何工作的。

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

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