Node Js First Program
In this tutorial, we are going to learn about printing our first program in node js. Further, we will print “Hello World” inside Node Code Environment (Node Shell) as well as run it on our self created server in node.js.
Recommended Books
Hello World in Node Js Shell
Node.js comes with an in built environment known as REPL (Node Code Testing Environment) which follows the model of Read-Eval-Print-Loop. REPL is the most convenient way of testing node js code. Follow a few steps to print “Hello World” in Node js.
- Launch the Node REPL in Windows (Command Line Prompt) and in MacOS (Terminal). Type node to make sure you want to execute the node js code.
- You can concatenate two strings together and print “Hello” + “World” in Node Js.
Performing Arithmetic Operations
You can also perform arithmetic operations like addition, subtraction, multiplication division etc. Let’s say if you want to add two numbers, you can test it by adding two numbers followed by the ‘>’ sign:
> 12+34 46
Similarly, you can multiply as well:
> 20*40 800
You can even store numbers inside variables and further use them for your arithmetic operations:
> var a = 10, b = 23; > a + b 33
After declaring variables, you will press ‘Enter’. At first it show ‘undefined’ as the returned result which implies that now you will guide the node REPL to further perform arithmetic operations on two variables. Let’s say you want to add them so will use the variables and add them together to see the result. In this way, variables get defined.
Hello World on HTTP Server
To create our hello world program on a server in Node.js, we will learn about the structure of creating servers first in node.js. These are the important requirements for creating an application:
- Importing Modules: To import modules in node.js we use ‘require’.
- Creating a Server: We then will a create a server which will listen/fetch the client’s request.
- Returning the User Request: Upon creating the server we then will read and return the request of the user on a browser or a console.
Let’s follow some steps to print Hello World on a local server created on our pc:
Creating a firstapp.js File:
- Create a file known as firstapp.js and save it on your computer local disk C:\Users\Your Name\firstapp.js
- Just add the following code to your firstapp.js file:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'); }).listen(3200);
var http is a variable name, it could be anything but since we are importing the ‘http’ module; it is a good practice to name our variable http as well. Next we then create a server by using the createServer() function. Lastly, we read and write the content that we want to see on the server listening at our designated port 3000 at localhost.
- After creating the firstapp.js, go to node shell (Terminal or Command Line Prompt) and run:
C:\Users\Your Name>node firstapp.js
- Now your computer is running like a server. To test the firstapp.js go to the localhost link at http://localhost:3200
- You will now see a ‘Hello World’ running