Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

The Node.js -- Part1: The Basic

node.js node javascript hello world

  • Please log in to reply
No replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 03 February 2013 - 03:21 AM

Recently I started learning Node.js and I'm thinking about writing a series of articles on Node.js. Today, we'll learn the basics of Node.js.
 
 
What is Node.js?
Node.js is an I/O driven application platform which consist of the V8 javascript engine which uses the google chrome browser engine, a library/framework and code is written in javascript. Its fast and used for creating scalable network based system. Node.js generally processes each operation asynchronously and it is event driven but we can adapt it for synchronous processing that means we can make a process synchronous as well as asynchronous as our need.


Why Node.js?

Node.js has some excellent advantages
  • node has non-blocking io,that means each io event must have at least one callback function which runs after the operation finished at that time other codes are running.
  • has support of mostly used protocol like http, dns, ssl etc
  • node is low level and it streams everything, its always streams the data but never forces data to be buffered.
Node is all over event driven io framework that lives with javascript events.
 
Setting Up Development Environment
Firstly we have to download node from here and install it.Then you are set to write node application. You will need an IDE or code editor like Netbeans IDE, notepad etc to write code. Node applications need some internal and external modules. Internal modules are come with node installation like http, fs (File System), url etc which is part of the Node.js platform. To run an application, as example written in a HelloWord.js file, open the command prompt on your system (better open Node.js provided command line which get installed with Node.js) and type node HelloWord.js.

Check out our other node.js content!

The Legacy Hello World Application
Node.js app is simply javascipt code snippets. Open the node.js command line interface to run code. Lets start checking our node environment by
 
//server.js 
console.log("Hi,The node server is running and up.");
So create a javascript file, namely server.js, in the current working directory with the code above.To run the code go to node provided CLI (command line interface) and run with node command
 
>node server.js
Which will show in the next line "Hi,The node server is running and up."
 
So our node environment is ready and you are set to develop your node application. Lets create our first http server. Node lives with node modules, to create http server we need the http modules. Modules are simply some collection of node functions created and exported for some specific purpose. We can use node modules by simply require() function.
 
//server.js file
var http = require('http');
http.createServer(function(request,response){
});
http.listen(8080);
console.log('Server in running..');
Run this by
 
>node server.js
Now our server created and ready to run and get requests from client side. Here by require() we imported the http module functions and here we used two of them createServer() and listen().
createServer() function takes a callback function as argument which takes two arguments request and response. We can process the request and response in the callback function as we want. The listen() function here takes the port number to accept connection to the client with the server address to 127.0.0.1 or localhost/ . Now if we hit http://localhost:8080 from browser the node server will start responding.
Here we just want to see the hello world so add the code in the callback function
 
//simple response to the request from the client
response.end("Hi,The Node server is running and up.");
So now if we hit http://localhost:8080 we can see "Hi,The Node server is running and up.". So our first step in node is at end.
 
 
Recommended Reading
There are lots of resources in the net. The http://nodejs.org   has the excellent documentation,the excellent http://www.nodebeginner.org/ and the http://book.mixu.net are enough to start.
 
Be back soon with the next one.
 
EDIT: Click me to go to second part of this series.

Edited by Roger, 19 February 2013 - 02:42 PM.
added link

  • 4