More Related Content Similar to Node.js ― Hello, world! の1歩先へ。 (11) Node.js ― Hello, world! の1歩先へ。18. C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
19. C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
20. C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
21. Node.JS
C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
22. Node.JS
C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
23. Node.JS
C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
29. Members
@meso @bad_at_math
“ ” “node.js ”
@Jack_ @KOBA789
@koichik
“Express/Socke.IO “ ”
“Node ” ”
869 *2011/10/16
@tnantoka
— beyond the “Hello, world!”
30. Members
@meso @bad_at_math
“ ” “node.js ”
@Jack_ @KOBA789
@koichik
“Express/Socke.IO “ ”
“Node ” ”
869 *2011/10/16
@tnantoka
— beyond the “Hello, world!”
31. @tnantoka
• Tobioka Tatsuya
- http://blog.bornneet.com/
- http://tatsuya.tobioka.net/
2008.4 Mitsubishi UFJ Information Technology, Ltd.
~ 2011.7 SE OnlineBanking
mindia, Inc.
2011.8 ~
Rails, Node, etc.
— beyond the “Hello, world!”
32. Node.js outputs
• Modules
- LooseLeaf.JS
- connect-fs, connect-sqlite
- Kanshi.js
- Public.js
• Articles
- Node
— beyond the “Hello, world!”
33. Node.js
— beyond the “Hello, world!”
35. • Node
— beyond the “Hello, world!”
36. • Node
- …
— beyond the “Hello, world!”
37. • Node
- …
• Hello, world!
— beyond the “Hello, world!”
38. • Node
- …
• Hello, world!
- Chat……
— beyond the “Hello, world!”
39. • Node
- …
• Hello, world!
- Chat……
• Web
— beyond the “Hello, world!”
40. • Node
- …
• Hello, world!
- Chat……
• Web
- WebSocket……… — beyond the “Hello, world!”
41. •
— beyond the “Hello, world!”
42. Node.js
— beyond the “Hello, world!”
44. Node.js
•
- $ java Hello
— beyond the “Hello, world!”
45. Node.js
•
- $ java Hello
- $ perl hello.pl
— beyond the “Hello, world!”
46. Node.js
•
- $ java Hello
- $ perl hello.pl
- $ node hello.js
— beyond the “Hello, world!”
47. Node.js
•
- $ java Hello
- $ perl hello.pl
- $ node hello.js
•
— beyond the “Hello, world!”
48. “ Node.js I/O
JavaScript
”
— beyond the “Hello, world!”
50. Node.js 2
“ Node.js I/O
JavaScript
”
— beyond the “Hello, world!”
51. Node.js 2
“ Node.js I/O
JavaScript
”
— beyond the “Hello, world!”
52. Node.js 2
“ Node.js I/O
JavaScript
”
— beyond the “Hello, world!”
53. 1
• NW
— beyond the “Hello, world!”
55. /
Apache
index.
html
— beyond the “Hello, world!”
56. /
Apache
request
index.
html
— beyond the “Hello, world!”
57. /
Apache
request Process/
Thread index.
1 html
— beyond the “Hello, world!”
58. /
Apache
request Process/
read
Thread index.
1 html
— beyond the “Hello, world!”
59. /
Apache
request Process/
read
Thread index.
1 html
request
— beyond the “Hello, world!”
60. /
Apache
request Process/
read
Thread index.
1 html
request Process/
Thread
2
— beyond the “Hello, world!”
61. /
Apache
request Process/
read
Thread index.
1 html
request Process/
Thread
2
request
— beyond the “Hello, world!”
62. /
Apache
request Process/
read
Thread index.
1 html
request Process/
Thread
2
request Process/
Thread
3
— beyond the “Hello, world!”
63. /
Apache
request Process/
read
Thread index.
1 html
return
request Process/
Thread
2
request Process/
Thread
3
— beyond the “Hello, world!”
64. /
Apache
request Process/
read
Thread index.
1 html
return
response
request Process/
Thread
2
request Process/
Thread
3
— beyond the “Hello, world!”
65. Node
index.
html
— beyond the “Hello, world!”
67. Node
request
read
index.
html
— beyond the “Hello, world!”
68. Node
request
read
index.
html
request
— beyond the “Hello, world!”
69. Node
request
read
index.
html
request
read
— beyond the “Hello, world!”
70. Node
request
read
index.
html
request
read
request
— beyond the “Hello, world!”
71. Node
request
read
index.
html
request
read
request
read
— beyond the “Hello, world!”
72. Node
request
read
index.
html
callback
request
read
request
read
— beyond the “Hello, world!”
73. Node
request
read
index.
html
callback
response
request
read
request
read
— beyond the “Hello, world!”
74. Node
request
read
index.
html
callback
response
request
read
request
read
I/O
— beyond the “Hello, world!”
75. • C10K
- ex) 2MB / proc. × 10000 http = 20 GB
•
- ,
• Web2.0 → Real Time Web
-
— beyond the “Hello, world!”
76. Why Node.js
• “Node's goal is to provide an easy way to
build scalable network programs. ”
• NW
- Node.js
•
- I/O…
— beyond the “Hello, world!”
77. •
•
Node.js
— beyond the “Hello, world!”
81. …
http://example.com/
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
82. …
request
http://example.com/
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
83. …
request
http://example.com/
response
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
84. …
request
http://example.com/
response
request
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
85. …
request
http://example.com/
response
request
kanshi.js http://example.jp/
timeout
http://example.io/
— beyond the “Hello, world!”
86. …
request
http://example.com/
response
request
kanshi.js http://example.jp/
timeout
request
http://example.io/
— beyond the “Hello, world!”
87. …
request
http://example.com/
response
request
kanshi.js http://example.jp/
timeout
request
http://example.io/
Internal server error
— beyond the “Hello, world!”
89. request
http://example.com/
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
90. request
http://example.com/
request
kanshi.js http://example.jp/
http://example.io/
— beyond the “Hello, world!”
91. request
http://example.com/
request
kanshi.js http://example.jp/
request
http://example.io/
— beyond the “Hello, world!”
92. request
http://example.com/
response
request
kanshi.js http://example.jp/
request
http://example.io/
— beyond the “Hello, world!”
93. request
http://example.com/
response
request
kanshi.js http://example.jp/
request
http://example.io/
Internal server error
— beyond the “Hello, world!”
94. request
http://example.com/
response
request
kanshi.js http://example.jp/
timeout
request
http://example.io/
Internal server error
— beyond the “Hello, world!”
95. 2
• JavaScript
— beyond the “Hello, world!”
97. JavaScript
•
ECMAScript
JavaScript ActionScript
browser Flash
DOM(HTML/CSS)
— beyond the “Hello, world!”
98. JavaScript
•
ECMAScript
JS
JavaScript ActionScript
browser Flash
DOM(HTML/CSS)
— beyond the “Hello, world!”
99. JS
• OS
- NW DB
CSJS <script src="node.js"></sciprt>
SSJS $ node -e "console.log('hello, world');"
> hello, world
— beyond the “Hello, world!”
100. Why SSJS
• JS
-
• JS
- Applet × Severside Java
— beyond the “Hello, world!”
101. • JS Node
• Ringo.js
- Java GAE
• Node
-
— beyond the “Hello, world!”
103. Node.js = ? 2
• Node JavaScript
— beyond the “Hello, world!”
105. Node.js = ? 3
• Node NW
• Node JS
— beyond the “Hello, world!”
106. Node.JS
C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
109. Observer
• 1
• http://observer.no.de/
•
•
— beyond the “Hello, world!”
111. nide
• 2
• Web IDE
•
•
http://coreh.github.com/nide/
— beyond the “Hello, world!”
112. Paragraph.JS
• http://paragraph.looseleafjs.org/
• 7 …
• Wiki
•
— beyond the “Hello, world!”
113. • 48
178
• Node.js
— beyond the “Hello, world!”
117. blosxom.js
• ^^;
• 2011/5
• Perl blog CGI
• daemon
— beyond the “Hello, world!”
120. Node.JS
C10K
WebScoket
Web Socket.IO
I/O JS
— beyond the “Hello, world!”
121. Node.js
— beyond the “Hello, world!”
122. • 10 blog
— beyond the “Hello, world!”
123. • nvm
- git clone git://github.com/creationix/nvm.git
~/.nvm
- echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile
- . ~/.bash_profile
• node (with npm)
- nvm install v0.4.12
- nvm alias default v0.4.12
— beyond the “Hello, world!”
124. Express
•
•
- $ npm install express -g
- $ express -s -t ejs blog
- $ cd blog
- $ npm install
— beyond the “Hello, world!”
125. Posts
•
GET /posts/new
POST /posts
GET /
— beyond the “Hello, world!”
126. node-sqlite
• Node SQLite
• $ npm install sqlite
• app.js
var db = new require('sqlite').Database();
db.open('blog.db', function(err) {
if (err) throw err;
db.query('SELECT * FROM posts', function(err, rows) {
if (err) throw err;
});
});
— beyond the “Hello, world!”
127. Table
• $ sqlite3 blog.db "CREATE TABLE posts
(title, body, date);"
title string
body string
date string
— beyond the “Hello, world!”
128. New post - View
• views/new.ejs
<h1><%= title %></h1>
<form action="/posts" method="post">
<p><input type="text" name="title" /></p>
<p><textarea name="body"></textarea></p>
<p><input type="submit" /></p>
</form>
— beyond the “Hello, world!”
129. New post - Logic(1)
•
app.get('/posts/new', function(req, res, next) {
res.render('new', {
title: 'New post'
});
});
— beyond the “Hello, world!”
130. New post - Logic(1)
•
app.post('/posts', function(req, res, next) {
var sql = 'INSERT INTO posts VALUES (?, ?, ?)';
db.execute(sql, [req.body.title, req.body.body, new Date().getTime()],
function(err, rows) {
if (err) return next(err);
res.redirect('home');
});
});
— beyond the “Hello, world!”
131. Index posts - view
• views/index.ejs
<h1><%= title %></h1>
<p><a href="/posts/new">New post</a></p>
<% posts.forEach(function(post) { %>
<section>
<h1><%= post.title %></h1>
<p><%= new Date(post.date) %></p>
<pre><%= post.body %></pre>
</section>
<% }); %>
— beyond the “Hello, world!”
132. Index posts - Logic
• app.js
app.get('/', function(req, res, next){
var sql = 'SELECT * FROM posts ORDER BY date DESC LIMIT ? OFFSET ?';
db.execute(sql, [5, 0], function(err, rows) {
if (err) return next(err);
res.render('index', {
title: 'Index',
posts: rows
});
});
});
— beyond the “Hello, world!”
133. Authentication
• Basic
• app.js
app.all('/posts/*', express.basicAuth(function(username, password) {
return username == 'admin' && password == 'pass';
}));
— beyond the “Hello, world!”
134. •
— beyond the “Hello, world!”
137. Server Side
• app.js
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('next', function(data) {
var sql = 'SELECT * FROM posts ORDER BY date DESC LIMIT ? OFFSET ?';
db.execute(sql, [1, data.offset], function(err, rows) {
if (err) return console.log(err);
socket.emit('next', rows[0]);
});
});
});
— beyond the “Hello, world!”
138. Client Side
<script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/jquery.js"></script>
<script src="/javascripts/bottom.js"></script>
<script src="/javascripts/script.js"></script>
$(function() {
var offset = 5;
var socket = io.connect('/');
socket.on('connect', function() {
socket.on('next', function(post) {
var section = '<section><h1>' + post.title + '</h1><p>' +
new Date(post.date) + '</p><pre>' + post.body + '</pre></section>';
document.body.innerHTML += section;
});
onBottom(function() {
socket.emit('next', { offset: offset++ });
});
});
});
— beyond the “Hello, world!”
139. Test
• insert.js
var db = new require('sqlite').Database(); var sql = 'INSERT INTO posts VALUES
db.open('blog.db', function(err) { (?, ?, ?)';
for(var i = 1; i <= 100; i++) { db.execute(sql, [title, body, new
(function(i) { Date().getTime() + i * 1000],
var title = 'Test ' + i; function(err, rows) {
var body = [ if (err) throw err;
i+' ', console.log('Inserted "Test ' + i + '"');
});
' ', })(i);
' ', }
'', '', '', '',
' '
].join('n');
});
— beyond the “Hello, world!”
140. •
— beyond the “Hello, world!”
142. Posts with railway
• $ npm install -g railway
• $ rw i rwblog && cd rwblog
• $ npm install -l
• $ rw g crud post title body
• $ rw s 3000
• $ open http://localhost:3000/posts
— beyond the “Hello, world!”
143. • LooseLeaf.JS
• Node
• $ npm install -g looseleaf@0.3.5
• $ looseleaf blog
• $ node blog/app.js
• demo: http://blog.looseleafjs.org/
— beyond the “Hello, world!”
146. “Node”
• ”Node”
• https://github.com/joyent/node/wiki/FAQ
• Node
— beyond the “Hello, world!”
147. beyond the this slide...
• Node.JS by Node.js_jp (Commig Soon!)
• node.js (@badatmath)
• Node.JS (@forest1040)
• (http://nodejs.jp/)
— beyond the “Hello, world!”
148. • CA , DeNA
• …
•
• Top Noder
— beyond the “Hello, world!”
149. •
• Node.js
• Join us !
— beyond the “Hello, world!”