In this article we will be discussing the very basics of node.js with some sample code snippets and examples.
Introduction :
node.js
is a platform built on chrome's v8 Javascript runtime for building
scalable, secure and fast network applications.It was created by Ryan
Dahl.
It is a server side javascript .It is evented , Non Blocking I/O system.It is specialised in concurrency control.
You can Download the package from here http://nodejs.org
Basic Architecture :
- The System uses event loop and does not exit until there is no other work to be done.
- It is Non Blocked system meaning that the system is capable of asynchronous communication (means can start handling other request without waiting for the response of the previous one).
- In other systems usually a thread is created for each request to the server. Since this thread based model has serious disadvantages like this ,thus this architectural design is dropped in node.js Basically it allocates heap for each connection.
Hello World Program in node.js
var http = require('http'); var conn = http.createServer(function(request,response) { response.writeHead(200,{'Content-Type':'text/plain'}); response.end('Hello World\n'); }); conn.listen(8000); console.log('Server running at http://127.0.0.1:8000/');
Note :Semicolons at the end of each statement is optional.
Explanation
: In the above program we are creating a simple web server using
node.js .In the program above we are including http module in the first
line.
Then we are creating a server using createServer
API which takes a callback function.So,every time a connection is made
to the server by the client this callback function is called which
writes headers to the client and ends the connection by writing Hello
world (usually the body).The program is listening on port 8000.
Run :
After you have downloaded and installed node.js on your system. Save
the file with the above code snippet as helloworld.js .Then in terminal
type
# node helloworld.js
This starts the server on port 8000 in localhost.
So open another terminal and telnet to the server as
# telnet localhost 8000
Hello world gets printed
In Browser....
http://localhost:8000
Building large scale network applications is simple and robust using node.js
I have implemented the echo server using node.js .You can find the code here
A simple chat server is also implemented . Can find the code here
All my Javascript code snippets with node.js examples and illustrations are available at
https://github.com/rShetty/jsbits
You can get the node package manager for installing various modules from here http://www.npmjs.org/
Express node.js is a popular web application framework for node.js
http://www.expressjs.com/
You can get the node package manager for installing various modules from here http://www.npmjs.org/
Express node.js is a popular web application framework for node.js
http://www.expressjs.com/
For any doubts or clarifications of the code snippets above do leave a comment or send me mail at coder.rshetty@gmail.com
No comments:
Post a Comment