Udemy logo

node-js-examplesJavaScript libraries have become increasingly popular over the last few years. One such library is the Node.js library. This library is an ingenious way to create real-time communication over the web using JavaScript. The communication is sent from one computer, the server retrieves the message and then sends the message to all recipient computers.

Learn Node.js through example at Udemy.com

There are several applications that use Node.js. Some HTML 5 games use Node.js to communicate real-time data as players play the game. Another popular application for HTML 5 and Node.js is chat rooms. Chat room applications typically used other technology years ago, but with Node.js, you can build a custom application that allows users to chat over the Internet using a web browser.

Starting Off with Node.js

You first need to download the Node.js libraries and include them in your project. Your script can be stored on your local desktop or server, or you can use a content delivery network (CDN) to store the file and link to the file from the CDN. CDNs are fast servers that use data centers to answer requests. These data centers are geographically closer to the user, so it speeds up your sites. CDNs have become more popular as user traffic grows, and you can also use them to deliver larger files such as images faster.

Deploy your Node.js applications to Heroku

The first example is just to ensure that your JavaScript is working properly in your browser. Do a quick check using the console app. The following code sends a quick “testing…” to the console:


Using Node.js with HTTP

Node.js is typically used to communicate with other users over the HTTP protocol. You are probably familiar with the HTTP protocol since it is the language of the web. You need to specify a protocol when you “hook” your server and client computers. The following example code hooks your Node.js to the HTTP protocol:

var con = require(‘http’);

The very basic Node.js code is creating a server. The server listens on a specific port and sends messages to the user. The server also responds with the proper server code that indicates that request was successful or unsuccessful. The typical server response is a 200 server response code. This response code is simply “OK.” It tells the requester that the request was retrieved and properly processed. The following code sets up the server:

var server = http.createServer( function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Request successful and processed\n");

The first part of this statement is the “createServer” function call that sets up the server to listen to requests. The function statement tells the server what to do when a request was retrieved. The “request” parameter is the request received and the “response” parameter is the response to return.

The next statement within the new function is the “writeHead.” This statement writes a server response to the requester. Notice the “200” parameter in the function parameters. This is the “OK” server status message that’s standard on the Internet when a page is properly processed and displayed. You use this same status code in your Node.js applications. The content type also tells the requestor what type of response is given. In this example, plain text is sent. You can also choose HTML as a response, which is the common response for a web page.

Get started with the ultimate guide to Node.js and Express

The last statement is what is sent back to the requester. In this example, the response “Request successful and processed” is sent. You would then need a statement in your application that would read this response and process the next request in your application. You would also need to deal with any errors returned by the server application.

You also need a port for the server to listen on. Ports are virtual connections that run a specific application when a recipient connects to the server. For instance, the HTTP protocol or the web language uses port 80. Port 80 is the universal port for web requests, but you can only use one listener for each port.  The following code tells your HTTP listener service to listen for requests on port 8081:


After you run this statement in your code, you should send a confirmation to the console to ensure that the code has run successfully. If it fails for any reason, the last console statement won’t run, so you know there is an issue. The following code runs a confirmation notification for your server application:

console.log( "Server running on port 8081 successfully. " );

Using Node.js on TCP

You can also use these JavaScript libraries on TCP. You use the same example code as the above, except you change a few simple lines. The following code is a Node.js example using TCP with the minor tweaks that makes it possible to run using the TCP protocol:

var con = require('net');
var server = net.createServer(function (socket) {
socket.addListener("connect", function () {
sys.puts("user connected on socket” + socket.remoteAddress);
socket.end(" Connection set up successfully. ");
server.listen(8080, "localhost");
console.log("TCP server has been set up on localhost and listening on port 8080.");

Notice the few minor changes especially the “localhost” difference. TCP allows you to communicate at a whole different level, because you can create real-time applications that don’t require a web server with this option. It’s a convenient way to create communication tools on private and public networks.

Understanding sockets and how they work with Node.js

The Node.js library of tools uses some of the newest JavaScript library technology, and it’s a powerful way to create fast real-time applications. There are several examples online, but you should first learn how to work with the libraries. If you are familiar with the JavaScript language, using the Node.js functionality should be a quick study.

Page Last Updated: March 2014

Node.Js students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo