Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http.createserver vs net.createserver in node.js

Tags:

node.js

tcp

I am having trouble understanding the difference between net.createserver and http.createserver in node.js.

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.

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 don't 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.

like image 332
intrepistar_88 Avatar asked Apr 25 '15 19:04

intrepistar_88


People also ask

What is HTTP createServer in node JS?

The http. createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.

What is Net module in node JS?

Node. js net module is used to create both servers and clients. This module provides an asynchronous network wrapper and it can be imported using the following syntax. var net = require("net")

What kind of server does the net module's createServer method create?

For example, net. createServer() may create a Unix domain socket and server.

How are node based Web servers different from traditional Web servers?

In Nodejs how node based web servers differ from traditional web servers? Node based server uses a single threaded model and can serve much larger number of requests compared to any traditional server like Apache HTTP Server. Node based server process request much faster than traditional server.


3 Answers

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.

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().

like image 50
Trott Avatar answered Oct 24 '22 18:10

Trott


I don't know much of a Node.js, but I know something about networks. HTTP is a protocol that works on 7th (Application) layer of model OSI. TCP is protocol that works on 4th (Transport) layer of model OSI. As you said, yes HTTP works on top of the TCP. The option of creating HTTP server by http.createServer() is there so you don't have to implement it by yourself by using net.createServer(). The protocol TCP might by used by lot of applications, you might create your own, or implement some different protocol than HTTP, for example: FTP, DNS, SMTP, Telnet and much much more.

like image 22
Ondřej Rehák Avatar answered Oct 24 '22 18:10

Ondřej Rehák


Straight from the Node Net documentation. NET is the basic bare-bones server you can create. It's particularly useful for setting up a cluster of servers and allows simple connections but on that you'll want communication protocols, namely HTTP, which HTTP is in fact a NET server at it's core.

The net module provides an asynchronous network API for creating stream-based TCP or IPC servers (net.createServer()) and clients (net.createConnection()).

And from the HTTP documentation. HTTP is the common way to transmit large sets of data as requested by the client and then a response is generated. It's the standard way of communicating over the internet and introduces the concept of handshakes and is done through REST protocol, you know the usual request and response way of communicating.

The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses — the user is able to stream data.

Websockets are an upgrade over the HTTP headers and offer low latency and less server load and are a much more minimal conversation. If you're talking peer to peer communication, that's the way you'll want to go.

like image 28
Fifth Dimension Dragon Avatar answered Oct 24 '22 17:10

Fifth Dimension Dragon