Let's Enjoy Node.js
    All in JavaScript




        Fred Chien
Enjoy Trip With 2 Rules

     旅程將啟,兩點規則
1
Buckle Up!
 請繫安全帶!
2
DO NOT KICK MY ASS!
  請你的朋友不要打我的頭!
說好不打臉的!
  說好喔!
WHO AM I ?

  我是誰?
Fred Chien
  錢逢祥
 永遠的大四生
Fred Chien
  錢逢祥
 永遠的大四生
Fred Chien
  錢逢祥
宅宅水電工
慾火焚身的男人
fred-zone.blogspot.com

  cfsghost @ gmail.com
JavaScript
   大調查
Web 開發者共同的迷思
 底層難!Porting 難!神人!大牛!
共同的夢想
JavaScript 成為海賊王!!!
JavaScript
Is NOT Browser-side Language Anymore

     是否不再只是瀏覽器端語言?
橡膠果實使一切成為可能
Run JavaScript without Browser

     不需要瀏覽器的 JavaScript
Old School JavaScript
Without Browser
With Node.js
More Third-party Modules
More Supports
換句話說
In other words
Enjoy JavaScript
      Enjoy It
Server-side Development
       開發後端應用
PHP/Perl/Python/JSP/ASP...
         開發後端應用
System Development
     開發系統程式
Services/Daemon/Syscall...etc

        控制作業系統的一切
更進一步
 Even More
All Development
   開發一切應用
About node.js
● Stable version is 0.6.15
● Support platforms:
  ○ MS Windows
  ○ Mac OS X
  ○ Linux
  ○ FreeBSD
node.js Features
● Based on V8 JavaScript Engine (With JIT)

● Stand-alone (Without Web Browser)

● Event-driven

● Non-blocking I/O model
More Good Things Come

● Extending with C/C++

● Support Multi-core Systems

● Provide Buffer Class
  ○ Solve 1.9GB heap limit of V8
  ○ Nice to binary data
Great Performance on Server
● Thousands of Concurrent Connections
● Minimal Overhead (CPU/Memory) on a
  singal process
Hello World
   快速試看看
Hello World! (helloworld.js)

var http = require('http');

http.createServer(function(req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello Worldn');
}).listen(10000);

console.log('Server running at http://localhost:10000/');
Run It!


$ node ./helloworld.js
Server running at http://localhost:10000/
Cluster Support (cluster.js)
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello worldn");
  }).listen(8000);
}
Run It!


$ node ./cluster.js
Worker 18394 online
Worker 18393 online
MongoDB Support (mongodb.js)
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017);
var db = new mongodb.Db('mydb', server);

/* open db */
db.open(function() {
  /* Select 'contact' collection */
      db.collection('contact', function(err, collection) {
        /* Querying (name == 'Fred Chien') */
            collection.find({ name: 'Fred Chien' }, function(err, data) {
                console.log('Name: ' + data.name + ', email: ' + data.email);
            });

      });

});
Run It!


$ node ./mongodb.js
name: Fred Chien, email: cfsghost@gmail.com
Write a XMPP Server (xmppsrv.js)
var xmpp = require('node-xmpp');

var c2s = new xmpp.C2S({
    port: 5222,
    domain: 'example.com'
});

c2s.on("authenticate", function(jid, password, client) {
    if (password == "12345678") {
        client.emit("auth-success", jid);
    } else {
        client.emit("auth-fail", jid);
    }
});

console.log('My XMPP Server was running at localhost:5222');
Run It!


$ node ./xmppsrv.js
My XMPP Server was running at localhost:5222
Node.js Modules
   你可以抓到更多的模組
NPM
Node Package Manager
8844+
Large Amount of Module
Get Modules with NPM


$ npm install mongodb
$ npm install node-xmpp
References

● Node.js Official Website:
  http://nodejs.org/

● NPM Official Website:
  http://npmjs.org/
Community in Taiwan

 ● Node.js Taiwan
   http://nodejs.tw/

 ● Facebook Fans Page
   # NodeJS.tw

 ● Node.js Taiwan Party in Taipei
   Once Every other Thursday
總結來說...
  Anyway...
前端開發者走入後端
 一種語言前後都可玩
Web開發者
將實現夢想
寫系統程式啦
低階程式讓專業的來
用 Node.js C/C++ Addon 打通硬體和系統的任督二脈
JSer 只要專心惡搞
 仍然不需要管底層的事
大秘寶就是我們的!
  我是海賊王!
Question?
   提問?
Thanks
感謝您的耐心耹聽

Let s Enjoy Node.js