img

NODE.JS HELLO WORLD

We will start our Node.JS journey with a simple hello world application. Our application will not do much but will lay a solid foundation that will make it easy for us to learn Node.JS.

In this article, we will create two simple hello world applications. One that displays the out in the console and the other that displays the information in the web browser.

Getting Started

Make sure that you have Node.js and NPM installed on your machine, if not, visit the Node.js website to install the latest version.

Let’s start by creating a folder for our app. In your favorite terminal run the following commands:

mkdir node

let’s now browse into the new directory using the following command:

cd node

Now, run the following command to create the directory for our first application

mkdir hello-world

run the following command to get into new directory hello-world

cd hello-world

run following command to initiate node js

npm init

after running above command, you need to answer the below questions,

The above command will create Package.json file in your project directory.

Open package.json file.

You can see the following information.

{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "Node js Hello world Application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mayank Patel",
  "license": "ISC"
}

NODE.JS Hello World Console Application

Let’s start with a hello-world project application.

Create a new file index.js in your project directory

Add the following code :

console.log("Hello World!");

NOW,

node index.js is the node command to execute your project file index.js.

Note: It is not necessary to specify file extension in command. By Default Node.js consider the file extension is.js You can run above command as node index too.

You will get the following results.

Hello World!

Congratulations. You just created your first Node.js application.

Now, what if a user wants to pass some inputs and display in our hello world! message? How can we do that? we can use the global module process that contains information about running Node.js application. Let’s see how we can do that.

Add Following code to index.js

console.log();
console.log(process.argv);
  • console.log(); prints a blank line
  • console.log(process.argv); displays an array of data that contains the command line arguments.

Let’s run the application and see what output print in the console.

node index --name=Tom

You will see following output printed in the console

[ 'C:\\Program Files\\nodejs\\node.exe',
  'C:\\node\\hello-world\\index',
  '--name=Tom' ]

Note:

we pass variable name with the value Tom.

Let’s now grab it and display in console message.

Replace the code for index.js with the following

function getArgument(argument){
    var index = process.argv.indexOf(argument);

    return (index !== -1) ? process.argv[index + 1] : null;
}

var name = getArgument('--name');

var message = name ? ("Hello " + name) : "Hello World";

console.log(message);

HERE,

  • function getArgument(argument){...}defines a function that accepts the command line argument name.That function returns null if argument name not found, function return value if argument found.
  • var name = getArgument('--name'); call the getArgument function with passing parameter “–name”. The result of the function we are storing in name variable.
  • var message = name ? "Hello " + name : "Hello World"; assigns the message “Hello World!” if the variable name is null.if it is not then the resulting print “Hello” and name variable value.
  • console.log(message); print the information in the console.

Let’s now test our application.

Run the following command

node index

You will get the following results

Hello World

Let’s now pass the name argument

node index --name "Mayank Patel :)"

You will get the following results

Hello Mayank Patel :)

You can find more information about Node.JS API from the official documentation.

We will create web-based hello world application in next Article.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

It is main inner container footer text