Getting started with Node.js

Getting started

First things first, you should install Node.js to build applications that leverage Node.js library. You can get the installer for Node.js from the official website. You can also go through the manual and documentation on Node.js there — it is detailed and includes code snippets wherever applicable.

Implementing an HTTP server in Node.js

In this section we will discuss how we can build our first Node.jsapplication and get it up and running. If you would like to build a simple HTTP Server you can just write the following code in a JavaScript file, save it with a name and you are good to go!

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Server started...");

Refer to the code snippet given above. The first statement includes the http module so that you can create a Http Server. The Http Server is created using the createServer() method and it listens for incoming connections at port 9000. When this code is executed, the Http Server starts and the message “Server started…” is display in the console.

To execute the script and get the server started, you need to invoke the script from the console using the following statement.
node IDGServer.js

Modules in Node.js

Node.js includes a collection of modules. Modules encapsulate a collection of related functions into one single unit. You can also export modules if you need to. The modules in Node.js provide a simple and easy to use API that can be used for File I/O, Networking (using HTTP, TCP, UDP, etc protocols), extend the Node.js library and implement Cryptography functions, etc. The following code snippet illustrates how you can export a module to make it available to other script files.

var exports = module.exports = {};

To import a module, you need to use the “require” keyword as shown below.

var moduleObj = require("./IDGCore.js");

You can know more on how you can create modules and import or export them as and when needed from this article.

Implementing a TCP server in Node.js

Let’s now build a simple TCP server using Node.js and consume it using C#. To build a simple TCP Server in Node.js here’s all the code you would need to write. Create a file called IDGServer.js and copy the following code into it.

var net = require('net');
var tcp_server = net.createServer(function(socket)

You can execute the script and run the TCP Server using the following statement in the console.

node IDGServer.js

The following piece of C# code can be used to read from the TCP socket we just created and display the text on the console.

public static void ReadData()
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("", 8000);
NetworkStream clientStream = tcpClient.GetStream();
byte[] data = new byte[4096];
int bytesRead = 0;
bytesRead = clientStream.Read(data, 0, 4096);
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine(encoder.GetString(data, 0, bytesRead));
catch(Exception ex)
//Process the error, write to logs, etc.

In my future posts here I would explore more on Node.js and show how you can use Node.js in enterprise applications.