Intro to Node.js

Using JavaScript on the server-side to build web sites

Hector Correa / http://hectorcorrea.com / @hectorjcorrea

Node.js logo

  • What is Node.js
  • Web Development with Node.js
  • Ecosystem (NPM, Express.js, MongoDB, node-inspector)
  • Paradigm (event-driven, non-blocking I/O model, async)

What is Node.js?

What is Node.js?

  • A server-side JavaScript interpreter
  • it uses the V8 JavaScript interpreter that Google wrote for Chrome...
  • ...and repurposes it for use on the server 1
  • Open source, created by Ryan Dahl (2009), available for Mac, Windows, and Linux, installer for Mac OS X is < 8 MB


1 http://www.ibm.com/developerworks/opensource/library/os-nodejs/

Samples

  • helloworld.js
  • webserver1/2/3.js (HTTP)
  • echoserver.js and chatserver.js (TCP)
  • express1.js - static files

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

Reference http://nodejs.org

Blocking


text = readFile("myfile.txt");
console.log(text);    			
					

Non-Blocking


readFile("myfile.txt", function(text) {
  console.log(text);  
});
console.log("xxx");   // executes immediately
					

Client-side example


$.ajax({
  url: "/js/fakedata.js",
  dataType: "json",
  success: function(data) {
    $("#txtName").val(data.name);
    $("#txtCity").val(data.city);
  },
  error: function(data) {
    alert("Oops...something went wrong" + data);
  }
});
        

Server-side example


var server = http.createServer(function(req, res) {

  if(req.url == "/sample.html") {

    fs.readFile("sample.html", function(err, text){
      res.setHeader("Content-Type", "text/html");
      res.end(text);
    });
    return;

  }

  res.setHeader("Content-Type", "text/html");
  res.end("Hello World");

});

server.listen(8000, "127.0.0.1");
					

The Node.js Ecosystem

Node Package Manager

  • Tool to download modules for Node.js
  • Comes bundled with Node.js
  • Over 20,000 30,000 modules available
  • Express.js, CoffeeScript, Socket.io, View Templates, Testing, Logging, DB Access, Async, RSS, Forever, Cluster, et cetera

# Usage
$ npm install someModule

Express.js

  • A server-side MVC framework to build web applications
  • Similar to Sinatra (Ruby) or Bottle (Python)
  • Provides the plumbing for HTTP, routes, and views
  • No built-in support for models

# Install Express
$ npm install express
					

Samples

  • express2.js - routes
  • express3.js - views
  • express4.js - external routes (via bookRoutes.js)
  • express4.js - database access (via bookRoutes2.js)
  • Blog web site (CoffeeScript)

Socket.IO

  • WebSocket is an IETF proposed standard that enables two-way communication. See http://tools.ietf.org/html/rfc6455
  • Socket.IO is a JavaScript library that wraps WebSockets
  • Socket.IO provides cleaner API than raw WebSockets
  • Provides support for older browsers

# Install Socket.IO
$ npm install socket.io
					

Samples

  • socketioDemo.js - server side
  • chat.html - client side

Debugging

Node Inspector is a debugger interface for Node.js
using the WebKit Web Inspector.


# Install node-inspector
$ npm install -g node-inspector

# Run your app
node --debug app

#Run node-inspector (in a separate window)
$ node-inspector

Browse to address given by node-inspector,
select source file to browse, set breakpoint,
and browse to your app.

Hosting in Production

  • Stand alone
  • Behind another web server (Apache, nginx, IIS)
  • PaaS: Nodejitsu.com, Heroku, AppFog, Azure

Questions?

Thanks!

References