Hello World Application in Node.js

Hi everyone welcome to my first blog. As everyone is quiet excited in learning the modern javascript framework and libraries. Lets get started with the simple application in node.js.

Introduction :

Nodejs is a javascript library .Moreover, we can say it is a javascript environment .Environment where every javascript code can be executed. Initially javascript was only available in browser. As javascript is a scripting language. After further development developer introduces Nodejs which can not only run in browser but also run as a standalone application on a machine.

Every javascript and nodejs code runs on the V8 javascript runtime engine .V8 engine compiles the javascript code into machine code (low level code) which is easily machine understandable.

Hence, javascript is not limited to only browser now . You can do much more with javascript now.Creating interactive web applications to mobile applications. Javascript is doing almost every thing now what other scripting languages are doing like python .
As i have given nodejs as an analogy to doctor Octavius. I will explain it later why?

Installation :

To get started with our first nodejs application .We need to install node on our machine .I am using a Ubuntu 20 ,so installation is pretty easy here too .Following snippets helps you in installation of nodejs .

sudo apt install nodejs

This command will let install the latest version of node js on your linux machine.I have already installed it so it has just given a pretty reminder.If you are a windows user , you can download and install nodejs setup from their official website Install nodejs on windows. Once it has been installed check the versions of nodejs and npm ( node package module ) on your machine.

npm is the world’s largest Software Registry which container contains over 800,000 code packages.Open-source developers use npm to share software.
Open-source developers use npm to share software.Many organizations also use npm to manage private development.organizations also use npm to manage private development.

Once you are done with installation , install any of the code editor sublime,atom or vscode on your machine . I will recommend visual studio code.I have already installed it on my machine.

Application :

lets create a new folder using mkdir command. I name this folder as firstApp.

mkdir firstApp

Move to the newly created folder using cd command followed by folder name .

cd firstApp

Initiate the node js application with npm init -y command . It will generate a package.json with configuration.

npm init -y

Lets visualize the content of current folder using ls command . It contains only one file .

ls

Lets open the current folder in visual studio code with code . command .

code .

Once packge.json file is successfully created lets create a server for this application . Create a file App.js using touch command .

touch App.js

Once the App.js file is created. Copy the following code in App.js file.

const http = require("http");const PORT = 3000; // creating a porthttp.createServer(function(request,response){ //creating httpserverresponse.writeHead(200,{"Content-type":"text/html"});
// writing content for head along with status code
response.write("<h1>Hello World</h1>"); // writing a html response.end();}).listen(PORT,function(){// exposing the server to the defined portconsole.log("Server is being listened on port ", PORT);});

here “http” is the built in module of Node JS through which we can transfer data over HTTP protocols. here is the list of all the built-in modules of Node JS https://nodejs.org/dist/latest-v8.x/docs/api/

Lets run the node server using the node <file_name> command .

node App.js

After running this command ,you will see an output on the terminal ,Server is being listened on port 3000. All you can do is open the browser and type in the url http://localhost:3000 and hit Enter key, Hello World will be shown in browser .

Now Press Ctrl+C in terminal to terminate the server . Add add “start” command in script of package.json file. Instead of running a server by file name , we will run it with command only this time . Add the following code in “scripts” object in package.json file .

"start" : "node App.js"

Now run the server using npm run start command .

npm run start

Congratulations!! you have successfully created you first node js server. The purpose of this article is to get you familiar with nodejs ,terminal ,scripts .
Stay tunned …..

To be updated soon .