->It´s web server is able to handle a HUGE number of connections out of the box
->Various libraries can be run on browser, the same as in the server
->Very friendly to Websockets (real-time web apps)
->Lots of libraries are being ported to it from other langs.
->Express, inspired in ruby´s Sinatra; is very light on memory but also very powerful
8. Benefits of Node.js
- No need to wait for the disk,
do something else meanwhile!
- Keep slow operations from
blocking other operations.
- To read info from disk, network, ... there must be a
callback
-Asynchronous event-driven model
9. Create Server Rquest
var http = require("http");
http.createServer(function (request, response)
{
response.end("<h2>This is the end!</h2>");
}).listen(3000, “127.0.0.1″);
->The require function will return an object representing the module that you
pass into it and you can capture that object in a variable.
->The createServer that takes a callback function and returns a new server
object.
10. Write HTTP headers.
var http = require("http");
http.createServer(function (request, response)
{
res.writeHead(200, {‘content-Type’:'text/html’});
response.end("<h2>This is the end!</h2>");
}).listen(3000, “127.0.0.1″);
->Then first need to write the appropriate HTTP headers.
->The writeHead function takes a couple of arguments. The
first is an integer value representing the status code of the
request which for us will be 200, in other words
11. write function
var http = require("http");
http.createServer(function (request, response)
{
res.writeHead(200, {‘content-Type’:'text/html’});
res.write(“<h1>Hello Word!</h1>”)
response.end("<h2>This is the end!</h2>");
}).listen(3000, “127.0.0.1″);
->Then we’ll call the write function and pass in the data
that you wish to send.
12. listen for new requests
var http = require("http");
http.createServer(function (request, response)
{
res.writeHead(200, {‘content-Type’:'text/html’});
res.write(“<h1>Hello Word!</h1>”)
response.end("<h2>This is the end!</h2>");
}).listen(3000, “127.0.0.1″);
->The listen function on our server object and pass in a
port number for it to listen on(3000). The listen function
also takes an optional second parameter which is the
hostname URL(“127.0.0.1″),
13. print out a message
var http = require("http");
http.createServer(function (request, response)
{
res.writeHead(200, {‘content-Type’:'text/html’});
res.write(“<h1>Hello Word!</h1>”)
response.end("<h2>This is the end!</h2>");
}).listen(3000, “127.0.0.1″);
console.log(“Listening on http://127.0.0.1:3000“).
->Finally, let’s print out a message to let us know that our
server is running
14. Run Program
There we go, now let’s run our app by calling node and passing
to it the name of the file we want it to execute.
node hello_world.js and open browser “http://127.0.0.1:3000“
That’s it. Enjoy………………..