Let s Enjoy Node.js
-
Upload
fred-chien -
Category
Technology
-
view
7.147 -
download
2
description
Transcript of Let s Enjoy Node.js
Let's Enjoy Node.jsLet's Enjoy Node.jsAll in JavaScript
Fred Chien
Enjoy Trip With 2 Rules
旅程將啟,兩點規則
1Buckle Up!
請繫安全帶!
DO NOT KICK MY ASS!請你的朋友不要打我的頭!
2
說好不打臉的!說好喔!
WHO AM I ?
我是誰?
Fred Chien錢逢祥
永遠的大四生
Fred Chien錢逢祥
永遠的大四生
Fred Chien錢逢祥
宅宅水電工
慾火焚身的男人
fred-zone.blogspot.com
cfsghost @ gmail.com
JavaScript大調查
Web 開發者共同的迷思
底層難!Porting 難!神人!大牛!
共同的夢想JavaScript 成為海賊王!!!
JavaScriptIs 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 JavaScriptEnjoy 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
● Extending with C/C++
● Support Multi-core Systems
● Provide Buffer Class○ Solve 1.9GB heap limit of V8○ Nice to binary data
More Good Things Come
● Thousands of Concurrent Connections ● Minimal Overhead (CPU/Memory) on a
singal process
Great Performance on Server
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 World\n');}).listen(10000);
console.log('Server running at http://localhost:10000/');
Run It!
$ node ./helloworld.jsServer 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 world\n"); }).listen(8000);}
Run It!
$ node ./cluster.jsWorker 18394 onlineWorker 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.jsname: Fred Chien, email: [email protected]
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.jsMy XMPP Server was running at localhost:5222
Node.js Modules你可以抓到更多的模組
NPMNode 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 Taiwanhttp://nodejs.tw/
● Facebook Fans Page# NodeJS.tw
● Node.js Taiwan Party in TaipeiOnce Every other Thursday
總結來說...Anyway...
前端開發者走入後端
一種語言前後都可玩
Web開發者
將實現夢想
寫系統程式啦
低階程式讓專業的來
用 Node.js C/C++ Addon 打通硬體和系統的任督二脈
JSer 只要專心惡搞
仍然不需要管底層的事
大秘寶就是我們的!
我是海賊王!
Question?提問?
Thanks感謝您的耐心耹聽