Web Programming TutorialsLearn How to Build Your First App in NodeJS

Learn How to Build Your First App in NodeJS

first-app

Node.js is a robust, open-source and cross-platform JavaScript runtime environment which is used for the development of JavaScript based tools and applications. It is not a JavaScript framework but most of its basic modules are coded in JavaScript which allow developers to write new modules in the JavaScript language. As a runtime environment, it uses Google’s V8 JavaScript engine to interpret the JavaScript code. It is completely free and used by thousands of developers globally due to its architecture level benefits. To Build your First App using Node.js

Node.js Architecture

  • It has an event-driven architecture that supports asynchronous I/O.
  • This architecture design optimizes the output and scalability in the Web applications which are operating on multiple I/O operations and built using the Node.js environment.
  • It has very good performance for real-time Web applications which includes real-time communication programs and various browser based games.
  • It is very useful for the development of I/O intensive web applications such as video streaming sites, single-page applications, etc.

In this chapter, we are first going to focus on the installation of Node.js from scratch on our machine/PC and then learn to build and set up our first application in NodeJS.

Installation of Node.js: Node.js installer for Windows or Macintosh can be downloaded from the following link – https://nodejs.org/en/download/

Once the windows installer is downloaded, then do the following steps.

1. Double Click on the installer (node-v4.4.7-x64.msi), you will notice the following dialogue box.
nodejs-setup-wizard
2. Click on the next button, the installer will display the following dialogue box asking you to accept the End-user License Agreement. Check the checkbox at the bottom and click on the Next button.
nodejs-end-user-license-agreement
3. You will notice the following dialogue box, asking you choose the destination folder to install the Node.js files. Choose the default destination folder path and click on the Next button as shown below.
nodejs-destination-folder
4. You will notice the following dialogue box which will ask you to select the custom setup. Keep the default settings and click on the Next button.
nodejs-custom-setup
5. You will notice the following dialogue box, click on the install button to initiate the installation of the Node.js.
ready-to-install-nodejs
6. Once the installation is completed successfully and this prompt is shown, click on the Finish button to complete the installation of Node.js on your machine.
complete-the-nodejs-setup-wizard

Building our first application in NodeJS
At this point, our installation of Node.js has been completed, and we are going to build our first application in NodeJS. We are using the eclipse IDE to build our Node.js application which involves the following steps.

Step 1: – Open eclipse and create a workspace by giving the suitable path (preferred path is the folder where you want to keep your project) on your machine as shown below.
workspace-launcher
Step 2: – On Eclipse, navigate as File -> New -> Others… It will open up a wizard. Select Node.js project from wizard as shown below.
select-a-wizard
Step 3: – Click on the Next button to enter the project name of your choice and click on the Finish button as shown below.
create-a-nodejs-project
Step 4: – At this step, we can observe that the project is successfully created in eclipse on the left hand side through the project explorer tab.
project-explorer-tab
Step 5: – Let’s start creating our first “Hello, Node JS!” application by using Node.js. We will discuss the various components of a Node.js application along with code example. There are three following core components for the Node.js application.

  • Import required modules – We can import required modules by using the require directive in order to load the Node.js modules. Here, we are using the require directive to load the http module and storing the returned HTTP instance into an httpInstance variable as shown below.
var httpInstance = require('http');
  • Server creation – It is a server that listens to the client’s requests i.e. the similar kind of job that the Apache HTTP Server does. Here, we are using the create http instance and calling ‘httpInstance.createServer ()’ method to create a server instance where we further bind it at the port 1234 using the listen method which is associated with the server instance. We are passing parameters requests and response to the handler method and using the ‘res.writeHead (200, {‘Content-Type’: ‘text/plain’});’ method to write the ‘text/plain’ response for the implementation that will return “Hello, Node JS!” thorough ‘res.end (‘Hello, Node JS!\n’);’ method.
httpInstance.createServer(function handler(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, Node JS!\n');
}).listen(1234, '127.0.0.1');
  • Reading the request and returning the response – We created a server which will read the HTTP request made by the client (i.e. browser, console, etc.) and return the response to the client as “Hello, Node JS!” as discussed above. When we run the application as ‘node hello-nodejs-server.js’ we can log the following string on the console by using ‘console.log ()’ method as shown below.
console.log('Server running at ');

Let’s consolidate the complete code together which will look as shown below.

Executing Node.js application

Open a command line and reach the Node.js directory location (here ‘C:\odesk\Abhishek Thakur\NodeJS\MyFirstNodeJSApplication’) and execute the following command.

C:\odesk\Abhishek Thakur\NodeJS\MyFirstNodeJSApplication>node hello-nodejs-server.js
Server running at 

nodejs-server
At this point, our first HTTP server will be up and running which is responding to all the HTTP requests at port 1234 at URL ‘http://127.0.0.1:1234/’

Next, open the browser and enter the URL as ‘http://127.0.0.1:1234/’, we can see the output string ‘Hello, Node JS!’ Printed on the web browser as shown below.
hello-node-js
Source code – Building the first application in Node JS

Conclusion
In this chapter, we have learnt about the installation of Node.js on our PC from scratch and then used eclipse as development IDE to build our first successful simple Node.js application. Node.js is a very popular runtime environment and is used by number of popular organizations such as eBay, General Electric, PayPal, Uber, Wikipins, GoDaddy, Microsoft, Yahoo!, Yammer, etc. Moreover, the popular Angular 2 framework operates on Node.js runtime environment.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -