Nodejs - Huihoodocs.huihoo.com/infoq/qcon-nodejs-20111021.pdf · Python: Tornado from...

Post on 16-Sep-2018

220 views 0 download

Transcript of Nodejs - Huihoodocs.huihoo.com/infoq/qcon-nodejs-20111021.pdf · Python: Tornado from...

Nodejs �������Javascript

Who am I?

Twitter: @fengmk2

Weibo: @Python��� , @FaWave

���EDP���@�

-4

1. Hello world F.��B8)Nodejs��/=MH Hello worldF.I��

2. String = Buffer => StreamString�Buffer, Buffer�Stream�C�;�'�A(;�'Javascript J1<�>�Socket9D�PA(EK��L�.@6 HTTP*!�

3. ?$&Q#0 JavascriptNodejs."��8)+�,�

4. �5�*!�5�*! �GO��@6�3Nodejs :���N"NL2�*!�%70�npm

Hello world����

Nodejs� �

��

�����

���

Nodejs , Tornado , Go , Netty

Nodejs: http://nodejs.org

Tornado: http://www.tornadoweb.org

Go: http://golang.org

Netty: http://www.jboss.org/netty

Python: Tornado

from tornado.ioloop import IOLoopfrom tornado.web import RequestHandler, \ Application

class MainHandler(RequestHandler): def get(self): self.write("Hello, world")

application = Application([ (r"/", MainHandler),])

if __name__ == "__main__": application.listen(8080) IOLoop.instance().start()

Go:

package mainimport ( "http" "io")func handler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello, world")}func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}

Java: Netty

private void handleHttpRequest( ChannelHandlerContext ctx, HttpRequest req) throws Exception { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, OK); res.setHeader(CONTENT_TYPE, "text/html; charset=UTF-8"); setContentLength(res, 11); res.setContent( ChannelBuffers.copiedBuffer( "hello world", CharsetUtil.US_ASCII)); sendHttpResponse(ctx, req, res);}

Nodejs:

var http = require('http');http.createServer(function(req, res){ res.end('Hello, World');}).listen(8080);

Nodejs: 4CPU

var cluster = require('cluster') , http = require('http');var server = http.createServer(function(req, res) { res.end('Hello World');});cluster(server).set('workers', 4).use(cluster.stats()).listen(8080);

����

CPU�$ cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz

���16GB

���$ cat /etc/issue | grep Linux

Red Hat Enterprise Linux Server release 5.4 (Tikanga)

����

$ ab -c 30 -n 1000000 http://127.0.0.1:8080/

������Name 30 threads

rps100 rps 1000 rps 5000 rps

Nodejs 7,287 7,038 6,786 6,912

Tornado 2,967 2,966 2,967 2,982

Go 5,214 5,157 5,080 5,164

Netty 13,526 13,351 11,332 7,921

Nodejs4P

14,826 14,332 12,161 8,287

** Nodejs������ **

String => Buffer =>

Stream

Javascript!�����%&

,�Javascript1+Unicode String

�)���!�

-��TCP��#�$" ��'(�43���,�Javascript���������

V8 1GB �/�%2

#�.�������!�'(0*� 1GB�.�����

Buffer

Buffer��8�"(���)10�6((9����"(��,*

3 V8��the V8 heap�+�2'�����$����V8 1GB �5#�.7

0 �!Buffer& �4-���%�/��$����0�#����

Buffer

// buffer.jsvar a = new Buffer(10);console.log(a, a.toString());

var b = new Buffer('QCon2011��');console.log(b, b.toString());

$ node buffer.js<Buffer 05 08 4e 00 2f 0f 26 05 04 4e> '\u0005\bN\u0000/\u000f&\u0005\u0004N'<Buffer 51 43 6f 6e 32 30 31 31 e6 9d ad e5 b7 9e> 'QCon2011��'

fs, net, http(s)

�sV/YS�var fs = require('fs')V/�I/OLhrPOSIX&M q���Ni����Q$;e 'R :p�K�

T=�s�var net = require('net')Q$;bp��T=�qC�

O�%�0!WC'6)���Ustreams�N_���(nQ$e �K)�

HTTP !Wc,6)��var http(s) = require('http[s]')Nodeo�HTTP17jH-I24<�;am�HTTP]d�(�P^��DJg.��PL5c�9?\@GB��i5AL8�>�[X�

"17�H-U��*�l#EF+Zf�k`g)25cc�����9M3�

fs, net, http(s)

fs�����

var fs = require('fs');// $ touch /tmp/helloSyncfs.renameSync('/tmp/helloSync', '/tmp/worldSync');var stats = fs.statSync('/tmp/worldSync');console.log('statsSync:' + JSON.stringify(stats));

fs, net, http(s)

fs�����var fs = require('fs');// $ touch /tmp/hellofs.rename('/tmp/hello', '/tmp/world', function (err) { if (err) throw err; fs.stat('/tmp/world', function (err, stats) { if (err) throw err; console.log('stats: ' + JSON.stringify(stats)); });});

fs, net, http(s)

����telnet���(1)// chat.jsvar net = require('net');

var clients = [];

net.createServer(function(client) {

client.write('Enter your name:\n');

client.once('data', function(data) {

var username = data.toString().trim();

clients.push(client);

broacast(username + ' : Join!\n');

client.on('data', function(data) {

var text = username + ' : ' + data;

broacast(text);

});

});}).listen(11021);

fs, net, http(s)

����telnet�(2)

// ���������function broacast(text) { console.log(text.trim()); var i = 0, l = clients.length; for(; i < l; i++) { var c = clients[i]; c.write(text); }};

fs, net, http(s)

����telnet���(3)

Server: $ node chat.jsmk2 : Join!mk2 : Hello qcon2011 hangzhou!

Client: $ telnet 192.168.1.xxx 11021Enter your name:mk2mk2 : Join!Hello qcon2011 hangzhou!mk2 : Hello qcon2011 hangzhou!

fs, net, http(s)

http servervar http = require('http');http.createServer(function(req, res) { if(req.url === '/') { res.end('Hello world'); } else { res.end('HTTP ' + req.method + ' : ' + req.url); }}).listen(8080);

fs, net, http(s)

http clientvar http = require('http');var options = { host: 'www.google.com', port: 80, path: '/'};http.get(options, function(res) { console.log("Got response: " + res.statusCode); res.on('data', function(data) { console.log(data.toString()); });}).on('error', function(e) { console.log("Got error: " + e.message);});

Stream: ���&

Stream���'5�7�-�2%0�2����$!"HTTP�/ �request'5����+:stdout��>(���#.��6'5�.���

��6'�����3���<���9�3�

,8��'EventEmitter�%��

42�$�*5��)�;���=��6�*��1� (Pipe)�

��#��fs.ReadStream

��(�#��

var readstream = fs.createReadStream(uploadfile);

!�� #��data���������$�)��"����&% ��')��'read� �&&�����

����fs.ReadStream

readstream.on('data', function(chunk) { console.log('write', chunk.length); // �������� req.write(chunk);});

�end��������������� readstream.on('end', function() {

req.end();

});

Pipe�"=�(#4'H��AdataTP�Aend8!�-��2$::pipe� WJ�Z=�2/ ��

W#7Rpipe�IL)�=��G1XN"@1�U=+�K(#4'�&�C<% ==> ;<%��<%�5D��[���req.end()readstream.pipe(req);

30O��$9QM�.V/ ��?S<%$�[���6*�B�readStream�5 F!�>�E,Y6�upload_file.js

Pipe������

:) �����

��� �

����

�"���fs.WriteStream

��$����

var writestream = fs.createWriteStream(savefile);

� &�� ��%�����!�����%���� ==> ���� #�����������������

res.pipe(writestream);

��� �fs.WriteStream

� ��������

writestream.on('close', function() { callback(null, res);

});

��WriteStream�����������download_file.js

�������Pipe

&��7�"�JavascriptNodejs!���$��� �

�%-4� *)��'�63

(��2 *����

#����1

�,�0

5��0

...+-./...

�����

�Web��

������ ��

� �����

......

����� ��IO��� ��

��Web �

job: �����������

���������

Trello: ���#�����3�� !����������� "����

��������

���� ���

PaintChat: ����������

��������

�������!"

Instagram Real-time API Demo: Instagram ���� ��!"��������%����$��&������ �#Socket.io� ����

�������

Cloud9: Web IDE���� ���

�������: Nodefox

Nodefox: ������������MySQL�������� ������ ���

�����: Nodefox

Nodefox�����

�������: LogglyLoggly: �syslog�HTTP �Web���

� ���

NAE Proxy: Node App Engine�Http proxy� �����������socket�

����: TermKitTermKit: next gen terminal / command application, built out of WebKit and Node.js.

����: TermKit

����: node-gui

@��� ��� nodejs��GTK+ demo

�����

Nodejs������������ � ��

1152����

�����

��Web�Database�Templating�CSS Engines�CMS�Buildand Deployment�Openssl, Hashing�SMTP� TCP/IP�RPC�Web Sockets & Ajax�Message Queues�Testing�JSON�XML�Debugging�Compression� Graphics�Payment Gateways�API clients�Control flow / Asyncgoodies�I18n and L10n modules ��� ����������������

������: npm

http://npmjs.org/

���� : npm

%npm����(

4427+�nodejs��� &!"��# �����'�!"���������$nodejs��mysql����

$ npm install mysql

����(1)

�Middleware�Connect ������HTTP server�������������

Web���Express, webjs

������node-mysql, redis-node,node-mongoskin

���(2)

Web Sockets & Ajax, RPC�Socket.io, nowjs

�����nodeunit, expresso

���/���� ��EventProxy, Jscex, async

Node's goal

Provide an easy way to build scalable

network programs.

� ������������

Q & A ����

next("Thanks ^_^")