node.c中的http.createserver vs net.createserver [英] http.createserver vs net.createserver in node.js
问题描述
我无法理解node.js中net.createserver和http.createserver之间的区别。
I am having trouble understanding the difference between net.createserver and http.createserver in node.js.
我已经阅读了位于这两个网址的两种方法的文档
https://nodejs.org/api/net.html#/net_net,https://nodejs.org/api/http .html#/ http_class_http_server 。
I have read the documentation for both methods located at these two urls https://nodejs.org/api/net.html#/net_net,https://nodejs.org/api/http.html#/http_class_http_server.
我知道http.createserver创建了一个http服务器,但文档说net.createserver创建一个tcp服务器。我明白tcp是http在其上的传输协议,并且http服务器设置为读取http请求头。我也很了解node.js中偶数排放器的概念。然而,我不明白这个tcp服务器的概念,为什么会在node.js中创建。上下文是我在node.js in action一书中编写一个聊天应用程序示例。
I understand that http.createserver creates an http server, however the documentation says that net.createserver creates a tcp server. I understand that tcp is the transmission protocol that http is on top of and that http servers are set up to read http request headers. I also understand the concept of even emitters in node.js pretty well. However I dont understand this notion of a tcp server and why one would be made in node.js. The context is I am coding a chat application example in the "node.js in action" book.
感谢任何帮助
推荐答案
http.createServer()
设置一个处理HTTP协议的服务器,这通过tcp确实传输。 net.createServer()
创建一个服务器,只需了解TCP连接何时发生,数据已被传输,等等,但不知道是否已经收到有效的HTTP请求等。
http.createServer()
sets up a server that handles the HTTP protocol, which is indeed transmitted over tcp. net.createServer()
creates a server that simply understands when a TCP connection has happened, and data has been transmitted, and so on, but doesn't know anything about whether a valid HTTP request has been received, etc.
如果您正在编写一个Web服务器,则有利于 http.createServer()
超过 net.createServer()
,因为它将为您节省大量工作。如果您正在编写其他类型的服务器,请不要使用 http.createServer()
。
If you are writing a web server, favor http.createServer()
over net.createServer()
as it will save you a lot of work. If you are writing some other kind of server, do not use http.createServer()
.
这篇关于node.c中的http.createserver vs net.createserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!