Node.js - Introduction

What is Node.js?





Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Node.js quite popular in the industry nowadays. Reasons for that is ,
  • It uses Javascript
  • It's very fast
  • It's huge ecosystem of open source packages (npm)
  • Great for real time services
Before learning Node.js you have to be familiar with Javascript, HTML and little bit of Command line Interface.

Installing Node.js

Before working with Node.js, you have to install node to your machince. For that first of all you have to go to the nodejs.org website and download the LTS version of node.js. And then open it up and install to your computer. 

First App

Ok! So, let's start writing our first node.js application.

Open your code editor. In my case it's visual studio code. Make a folder called firstapp and inside that create a file called app.js. Then inside that file type 

console.log("Hello World!");

Now open up the terminal in that folder and type following command.



If you get "Hello World!" as the output in the console. Then you have installed node correctly to your computer and you have created your app.js file correctly.

Creating your first server

In node.js applications, we write servers instead of using some servers such as Apache. So let's see how to write a server and how to take an output to your browser window.

First remove the code which we have previously added to the app.js file. The add below code.

var http = require('http');

function onRequest(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World!');
response.end();
}

http.createServer(onRequest).listen(8000);

Oh crap! What is this? Let me explain. 😋

By writing the very first line of the code, we have imported the "http" module and assign it to the http variable. In nodejs we use modules because we need to import some functionalities which will require to run our code.

This http module will give us access to the createServer method. What does this method do? yeah exactly! It will create a server.

http.createServer().listen(8000);

.listen(8000) - This method will say to the server we just created, to listen the port 8000. So all the requests which are coming to the port 8000 will handle by the server. But currently our server is doing nothing.

In order to specify our server to do something with the requests (handle the requests), we can use any method inside the createServer() method. Let's create a method called "onRequest". You can use any name you like.

http.createServer(onRequest).listen(8000);

What will this onRequest function do?

function onRequest(request, response){
//content
}

request and response are the two arguments to this function. In side this function we can do changes to the requests, and responses. In this example I will only work with the response.

So inside the function, let's add the below three lines of code.

response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World!');
response.end();

From the first line it has change the header of the response by changing response code and the type of the content of the response.

Second line, I have added the plain content which is "Hello World!". If we use html as the content type we can add html content/files here. You will learn those in my upcoming nodejs tutorials.

In final line we have end the response.  Then the response can be send to the person/computer who is requesting this content.

Now save the file, and in the terminal, run "node app" as before. You will get nothing as the the output in the terminal. But, go to your browser and open up a new tab and enter http://localhost:8000 

Then, you will see "Hello World!" in your browser window.



So, That's all for now . Let's meet with the next tutorial on Node.js soon. 

Thank you!

Kalpani

Comments