Contenu connexe Similaire à Всеволод Струкчинский: Node.js Similaire à Всеволод Струкчинский: Node.js (20) Всеволод Струкчинский: Node.js8. 1
2 on mouseUp
3
get the selection
4
if it is empty then as "Dial what number?"
5
if it is not empty then
6
push this card
7
visual effect zoom open
8
go to stack "Phone"
9
dial it
10
pop card
11
end if
12 end mouseUp
13
1987
14. Расчитан на обработку событий
10
Был написан за
дней
Function as first-‐‑class objects
Прототипное наследование из Self
1995
34. Ryan Dahl
I/O has to be done differently.
We are doing it wrong
35. Ryan Dahl
I/O has to be done differently.
We are doing it wrong
bit.ly/jsconf-‐‑node
120. require("ʺantigravity"ʺ)
in /home/user/programm/library/index.js
1. /home/user/programm/library/node_modules/antigravity
2. /home/user/programm/node_modules/antigravity
3. /home/user/node_modules/antigravity
4. /home/node_modules/antigravity
5. /node_modules/antigravity
NODE_PATH
$HOME/.node_modules, $HOME/.node_libraries, $PREFIX/lib/node
139. Circular dependencies
a.js
1
2
3
4
module.exports.hello = "Hello from A";
var b = require('./b.js');
module.exports.helloFromB = b.hello;
console.log("B: ", b);
b.js
1
2
3
4
module.exports.hello = "Hello from B";
var a = require('./a.js');
module.exports.helloFromA = a.hello;
console.log("A: ", a);
153. package.json
{
}
"name": "express",
"version": "3.4.4",
"dependencies": {
"connect": "2.11.0", "commander": "1.3.2", "range-parser
},
"devDependencies": {
"ejs": "*", "mocha": "*", "jade": "0.30.0", "hjs": "*", "
},
"main": "index",
"bin": {
"express": "./bin/express"
},
"scripts": {
"prepublish": "npm prune",
"test": "make test"
},
"engines": {
"node": "*"
}
162. joyent/libuv -‐‑ loop.c
1 static int uv__loop_init(uv_loop_t* loop, int default_loop) {
2
// ...
3
RB_INIT(&loop->timer_handles);
4
QUEUE_INIT(&loop->wq);
5
QUEUE_INIT(&loop->active_reqs);
6
QUEUE_INIT(&loop->idle_handles);
7
QUEUE_INIT(&loop->async_handles);
8
QUEUE_INIT(&loop->check_handles);
9
QUEUE_INIT(&loop->prepare_handles);
10
QUEUE_INIT(&loop->handle_queue);
11
QUEUE_INIT(&loop->pending_queue);
12
QUEUE_INIT(&loop->watcher_queue);
13
14
err = uv__platform_loop_init(loop, default_loop);
15
// ...
16 }
165. joyent/node -‐‑ core.c
: 258
1 int uv_run(uv_loop_t* loop, uv_run_mode mode) {
2
r = uv__loop_alive(loop);
3
while (r != 0 && loop->stop_flag == 0) {
4
// ...
5
uv__update_time(loop);
6
uv__run_timers(loop);
7
uv__io_poll(loop, timeout);
8
// ...
9
}
10 }
1
2
3
4
5
static int uv__loop_alive(uv_loop_t* loop) {
return uv__has_active_handles(loop) ||
uv__has_active_reqs(loop) ||
loop->closing_handles != NULL;
}
176. Fibonacci
1
2
3
4
5
6
7
8
9
10
11
12
function fibonacci(i, n, f1, f2, res) {
if (i >= n)
res.end(n + ": " + (f1 + f2));
else
setImmediate(function() {
fibonacci(i + 1, n, f2, f1 + f2, res);
});
}
require('http').createServer(function (req, res) {
fibonacci(0, 1473, 1, 1, res);
}).listen(8005);
183. Streams in Node.JS
1. fs -‐‑ createReadStream, createWriteStream
2. process -‐‑ stdin, stderr and stdout
184. Streams in Node.JS
1. fs -‐‑ createReadStream, createWriteStream
2. process -‐‑ stdin, stderr and stdout
3. http -‐‑ client requests, server response
185. Streams in Node.JS
1. fs -‐‑ createReadStream, createWriteStream
2. process -‐‑ stdin, stderr and stdout
3. http -‐‑ client requests, server response
4. tcp -‐‑ sockets
186. Streams in Node.JS
1. fs -‐‑ createReadStream, createWriteStream
2. process -‐‑ stdin, stderr and stdout
3. http -‐‑ client requests, server response
4. tcp -‐‑ sockets
5. zlib -‐‑ createUnzip...
187. Streams in Node.JS
1. fs -‐‑ createReadStream, createWriteStream
2. process -‐‑ stdin, stderr and stdout
3. http -‐‑ client requests, server response
4. tcp -‐‑ sockets
5. zlib -‐‑ createUnzip...
6. crypto -‐‑ createCipher
189. Crypto service
1
var app = require('express')();
1
2
3
4
app.post('/:algo/:secret', function(req, res) {
// Some magic should be applied here
res.end(req.params.algo + " " + req.params.secret);
});
190. Crypto service
1
var app = require('express')();
1
2
3
4
app.post('/:algo/:secret', function(req, res) {
// Some magic should be applied here
res.end(req.params.algo + " " + req.params.secret);
});
1
app.listen(31337);
195. Crypto service
1
var app = require('express')();
1
2
3
4
5
app.get('/:algo/:secret', function(req, res) {
var cipher = require('crypto')
.createCipher(req.params.algo, req.params.secret);
req.pipe(cipher).pipe(res);
});
1
app.listen(31337);
210. Writeable stream
backpressure
1
var backpressure = process.stdout.write('data');
This return value is strictly advisory. You MAY continue
to write, even if it returns false. However, writes will be
buffered in memory, so it is best not to do this
excessively. Instead, wait for the drain event before
writing more data.