Node net module basic examples and more
This post will be on the node net module that can be used to accomplish tasks involving the tcp protocol rather than http. The net module includes constructors that return insistences of streams that allow for data to be transmitted on a per data chunk basics like that of the createReadStream method in the file system module.
1 - Node net basic example
For a basic example of the nodejs net module here is a hello world example of using the create server net method to set up a TPC server on localhost at port 8080.
|
|
Once this example is up and running I can use a TCP client like telnet to connect to it an sure enough I get the message hello world. However if you do not have a TCP client installed it is also possible to connect to the server with another script so lets look into how to do that as well.
2 - Using the Socket Constructor to connect to a server
So the socket constructor can be used to connect to a server by creating and instance of socket and then connecting to a server by using the connect method of socket.
|
|
3 - Conclusion
So the net module can be used to set up and connect to TCP servers, it is also the base class for the create server constructors used in the http module as well.