Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Node.js Tutorial: Step-by-Step Guide For Getting Started

Node.js Tutorial: Step-by-Step Guide For Getting Started

 July 14  | 0 Comments

This post is focused at beginners with prior knowledge in JavaScript and will give you a clear picture on how to get started with NodeJS.

The popularity of JavaScript applications has been skyrocketing in the last few years, with Node.js definitely facilitating this growth, as compared to Ruby, Python, and Java.

Get Skilled in Full Stack Web Development

Node.js is an open-source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.

Node.js was invented in 2009 by Ryan Dahl and other developers working at Joyent. Node.js was first released in 2009 and supported only Linux.

Node.js also provides a rich library of various JavaScript modules which simplifies the development of web applications to a great extent.

Hence, Node.js is considered to be an amalgamation of Runtime Environment + JavaScript Library.

Node.js = Runtime Environment + JavaScript Library

Let’s begin with Installing Node.js in a Window.

 

Here is step by step process for Installation

Step1: Download the NodeJS apk from official website of nodeJS i.e. https://www.nodejs.org

Step2:  After downloading, double-click on the setup file to start the Node.js Setup Wizard. Click Next.

 

Step3: If you accept the End-User License Agreement, check “I accept the terms in the License Agreement”. Click Next.  Click Next

Step4: Then specify the destination file.

 

Step5: Click Next

 

Step6: Click Install

 

Step7: Finally your Node.js is installed.

 

What is NPM?

  • NPM is the package manager for JavaScript and is a default for Node.js.
  • It is bundled and installed automatically with the environment. NPM runs through the command line and manages dependencies for an application. It also allows users to install Node.js applications that are available on the NPM registry.
  • NPM makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you’re sharing.
  • The node comes with NPM installed so we should have a version of NPM. The version of the NPM can be checked by using the following command:

npm –version

  • The NPM version can be updated with the following command:

sudo npm install npm –g

npm –version

If you have installed node.js correctly then, you should be able to invoke the interactive node.js shell like this:

$ node
> console.log('Hello World');
Hello World

Running Programs Through Node.js

After downloading and installing the needed software, the program can be run via Node.js. The steps are as follows:

  1. Open the OS Command Prompt (cmd).
  2. Navigate to the folder where the .js file is located.
  3. Type in the following:

node [name-of-the-file].js

  1. Click [Enter].
  2. The program should now display the result.

Now, writing a node.js program is as simple as creating a new file with a ‘.js’ extension.

Program1:

Let’s start with a simple hello world http server.

To write a program that responds to hello world via http. We’ll call the file ‘hello_http.js’ and put the following code into it:

// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

 

Now to run this program from the command prompt type the following:

$ node hello_http.js

You will get the output as “Hello World”

  • Now let’s look into testing the Server.

Testing the server is as simple as opening a new browser tab, and navigating to the following url : http://localhost:8000/. As expected, you should see a response that reads: ‘Hello World’.

Now let’s discuss the steps involved in hello world http server program.

  • In the first line, we include the http core module and assign it to a variable called http.
  • Next, we create a variable called ‘server’ by calling http.createServer. The argument passed into this call is a closure that is called whenever an http request comes in.
  • Finally, when we call server.listen(8000) to inform node.js the port on which we want our server to run.

Program 2:

Now lets us look into a program to find if a number is even or odd

var http = require('http');
//variable declaration
var num = process.argv[2];
var server = http.createServer(function (request, response) {
 response.writeHead(200, {"Content-Type": "text/plain"});
 if (num % 2 === 0 ) {
  console.log("Given number" + " " + num + " "+ "is even number");
  response.end("Given number" + " "+ num + " "+"is even number");
 } else {
  console.log("Given number" +" "+ num + " "+"is odd number");
  response.end("Given number" + " " + num + " "+ "is odd number");
 }
});
server.listen(3000);
console.log("Server running at http://127.0.0.1:3000/");

This program takes input from the user from the line var num = process.argv[2];

The user will have to enter the number through the keyboard.

Debugging node.js apps

There are many ways to debug your node.js, based on applications. However, if you have to locate a tricky bug in an existing applications, here are some approaches that can be helpful:

  • Using console.log()

The easiest way to understand a problem is by inspecting objects using console.log(). You can directly pass in objects as parameters:

var foo = {'Hello Acadgild'};
console.log(foo);

 

2.  Using the node debugger

You can invoke the debugger by simply using the following command:

$ node debug my_file.js

Conclusion

Now, you possess the basic knowledge of Node.js and how to get started with node.js. Since node.js is a major component of wed development, it is worth the time, money and effort developers to learn node.js. Node.js is similar to PHP and is primarily used to build network programs such as web servers. In our next post we will be discussing about why NodeJS is better than PHP.

Keep visiting our site www.acadgild.com for more updates on Bigdata and other technologies. Click here to learn NodeJS from our Expert Mentors

 

>