node.js

14
Node.js – Web Framework By:Pravin Mishra [email protected] node.js Hello World

Upload: pravin-mishra

Post on 25-Jun-2015

1.756 views

Category:

Technology


0 download

DESCRIPTION

->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

TRANSCRIPT

Page 1: Node.js

Node.js – Web Framework By:Pravin Mishra [email protected]

node.js Hello World

Page 2: Node.js

Contents

• Why Node.js?

• Node Overview

• Benefits of Node.js

• Create Server Request.

• Write HTTP Header.

• listen for new requests .

• print out a message.

• Run program.

• Questions?

Page 3: Node.js

Why Node.js?

->Node's goal is to provide an easy

way to build scalable network

Programs.

->Keep slow operations from

blocking other operations.

Page 4: Node.js

What is Node.js?

->Standard library.

Timers, Process, Events, Util, Buffer,

Crypto, TLS/SSL, FileSystem, Net, DNS,

HTTP/HTTPS, URL, UDP.

-> I/O needs to be done differently

Page 5: Node.js

Templating

• Jade.

• EJS.

• Haml.

• Sass

Page 6: Node.js

Database

• Mysql

• Redis

• MongoDB

• CouchBD

Page 7: Node.js

Deploy

• Heroku

• Nodejitsu

• Cluster

Page 8: Node.js

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

Page 9: Node.js

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.

Page 10: Node.js

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

Page 11: Node.js

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″),

Page 12: Node.js

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

Page 13: Node.js

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………………..

Page 14: Node.js

THANKS

Questions?

By:Pravin MishraRails/UI Developer

[email protected]