Getting Started With NodeJS: Installation And A Simple Example

By now, I believe we have learned the internals of NodeJS from my series of articles on NodeJS.

If someone asks about NodeJS and its three main components, EventLoop and when to use choose and when not to choose NodeJS for developing network applications, we could tell them if we are clear on my NodeJS articles.

This article is mainly for beginners to learn how to install and get started with NodeJS.

Installing NodeJS on Windows

Since NodeJS is an pen source platform, we can download the code from github. To build the source code we need:

  • Python 2.6 or 2.7
  • Visual Studio 2010 or higher

Now, open the Visual Studio Command Prompt to build the release.

Type: vcbuild release.

If it fails, please be sure you have set the PATH env variable for Python.

If you don't want to build the source, then just the download the latest builds from the following link: http://nodejs.org/download/.

Once you are ready with the installation, just proceed with your favorite IDE. I am familiar with Visual Studio as well as SublimeText.

Creating your first Web Server

In this article and my future posts, I will use SublimeText as the IDE for NodeJS. So just open the Node command line interface, navigate to the folder in which you will have the main JavaScript file, usually app.js or server.js.

Now start by creating the package.json file first. In the command line interface, type as below:

Creating your first Web Server

The command npm init creates package.json in your root folder prompting some details about the node project that you are going to create. The package.json file contains the details as shown in the preceding picture.

If you want to learn more about the package.json file, please visit: package.json.

Now, create a new JavaScript file with the name server.js then copy and paste the following code into the server.js.

  1. var http = require('http');  
  2. http.createServer(function (req, res) {  
  3.   res.writeHead(200, {"Content-Type""text/html"});  
  4.   res.write("<!DOCTYPE "html">");  
  5.   res.write("<html>");  
  6.   res.write("<head>");  
  7.   res.write("<title>Hello World Page</title>");  
  8.   res.write("</head>");  
  9.   res.write("<body>");  
  10.   res.write("Hello World!");  
  11.   res.write("</body>");  
  12.   res.write("</html>");  
  13.   res.end();  
  14. }).listen(1234, '127.0.0.1');  
  15. console.log('Server running at http://127.0.0.1:1234/');  
In the code above, the first line requires the NodeJS's builtin module “http”. 
  • From nodejs.org site: The HTTP interfaces in Node are designed to support many features of the protocol that 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.

  • In the second line, we are creating a http server that accepts an array of callback functions. The callback function will be executed for every request. The callback function accepts two arguments, request and response, respectively.

  • In the callback function, we are writing the header value "Content-Type" as HTML and then we are writing the HTML content.

  • As a chaining function for the createServer function, we are calling the listen function with the port number 1234 and ip address 127.0.01 to listen on the port and ip address given.

  • In the last line, we are logging the information into the node's console.

To run the example, type the following command in the command line interface:

node server.js

After hitting Enter, you will see the message “'Server running at http://127.0.0.1:1234/” displayed in the console. Now the web server is listening on the port 1234 on the localhost. Open a browser and type localhost:1234/ in the address bar. You wills see the HTML page with the “Hello World” message.

example output

Some links for NodeJS

If you want to contribute to the NodeJS, please visit: NodeJS.

To get in-depth knowledge, please visit: Node.js v0.10.33 Manual & Documentation.

Conclusion

In this article, we have discussed the NodeJS installation and the simple web server example. In the next article, we shall discuss the various frameworks available for the NodeJS platform.

Thanks for reading.

Happy Coding.


Similar Articles