Showing posts with label nodejs_0-10-38. Show all posts
Showing posts with label nodejs_0-10-38. Show all posts

Saturday, August 15, 2015

First learning Node.js

We will learn another software today, Node.js. Another word that I came across many times when reading on information technology articles. First, let's take a look on what is Node.js. From the official site,

Node.js® is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

So this is very much to understand what exactly is Node.js from that two sentences but as you continue to read in this article, you will get some idea. If you have basic javascript coding experience, you will think Node.js is just a script that run goodies stuff on browsers to enhance people experience. But as javascript envolve, Node.js evolve into an application where you can code as a server application! We will see that later in a moment.

Okay, let's install Node.js. If you are using deb base linux distribution, for example debian or ubuntu. It is as easy as $ sudo apt-get install nodejs. Otherwise, you can download a copy from this official site and install it.

Let's start with a simple Node.js hello world. Very easy, create a helloworld.js and do the print. See below.

 user@localhost:~/nodejs$ cat helloworld.js   
 console.log("Hello World");  
 user@localhost:~/nodejs$ nodejs helloworld.js   
 Hello World  
 user@localhost:~/nodejs$   

very simple, one liner produce the hello world output. You might ask, what can Node.js functionalities can I use other than console. Well, at the end of this article, I will give you the link so you can explore further. But in the meantime, I will show you how easy to create a web server using Node.js! Let's read the code below.

 user@localhost:~/nodejs$ cat server.js   
 var http = require("http");  
 http.createServer(function(request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});  
  response.write("Hello World");  
  response.end();  
 }).listen(8888);  
 console.log("create a webserver at port 8888");  
 user@localhost:~/nodejs$ nodejs server.js   
 create a webserver at port 8888  

As you can read,  we create a file called server.js require a module called http. We pass an anonymous function into the function createServer of http module. The response will return http status 200 with a hello world. You can try to access in your browser with localhost:8888. Notice that the execution of the Node.js continue after http is created, unlike other language which will wait the execution finish before proceed the next line of code, Node.js execution will continue and this make Node.js asynchronous.

Well, by now you should understand what Node.js can do for you and if you interest more on Node.js , I will leave you this very helpful link.