node.js
N00b no more!

A lightweight introduction to node.js

  1. n00b
  2. beginner
  3. intermediar
  4. ninja

Event-driven, non-blocking I/O model

Similar in design to Event Machine in Ruby, Twisted in Python or React in Php

  • Server-side Javascript
  • Code executed by the V8 engine
  • Single-threaded [1]
  • System bindings written in C++
  • Started in 2009
  • First Released in 2011

1: multiple threads for file and network events

INSTALL

Use installer for Windows or Mac or package manager for Linux

$ bash hello-world.sh

$ java hello-world.jar

$ php hello-world.php

$ python hello-world.py

$ ruby hello-world.rb

$ node hello-world.js

hello-world.js


                    console.log('Hello World!');
                        
Hello World!

hello-www-world.js


            var http = require('http');

            http.createServer(function (request, response) {
              response.writeHead(200, {'Content-Type': 'text/plain'});
              response.send('Hello World!\n');
            }).listen(1337, "127.0.0.1");

            console.log('Server running at http://127.0.0.1:1337/');
                    
Server running at http://127.0.0.1:1337/
Hello World!

                var http = require('http');
                    

require

Core Modules

assert, buffer, childprocess, cluster, console, constants, crypto, debugger, dgram, dns, domain, events, freelist, fs, http, https, linklist, module, net, os, path, punycode, querystring, readline, repl, stream, stringdecoder, sys, timers, tls, tty, url, util, vm, zlib,

https://github.com/joyent/node/tree/master/lib

node package manager

npm <command> [args]

$ npm install ________
$ npm install express
  1. n00b
  2. beginner
  3. intermediar
  4. ninja

Express

hello-www-world-express.js


            var express, app, server;
                
            express = require('express');
            app = express();
            
            app.get('/hello-world.txt', function(request, response){
                response.send('Hello World!');
            });
            
            server = app.listen(1337, function() {
                console.log('Listening on port %d', server.address().port);
            });
                    
Server running at http://127.0.0.1:1337/
Cannot GET /

hello-www-world-express.js


            var express, app, server;
                
            express = require('express');
            app = express();
                        

            app.get('/hello-world.txt', function(request, response){
                response.send('Hello World!');
            });
                        

            server = app.listen(1337, function() {
                console.log('Listening on port %d', server.address().port);
            });
                        
Hello World!
Grunt JsHint CssLint Bower
The JavaScript Task Runner Detects errors and problems in JavaScript code Points out problems with your CSS code A package manager for the web

Get it

Try it

Grok it