Skip to content

Commit 2ee5013

Browse files
author
clydezh
committed
change
0 parents  commit 2ee5013

File tree

434 files changed

+7396
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

434 files changed

+7396
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

chapter1/代码清单1-1/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var http = require('http');
2+
http.createServer(function (req, res) {
3+
res.writeHead(200, {'Content-Type': 'text/html'});
4+
res.write('<head><meta charset="utf-8"/></head>');
5+
res.end('你好\n');
6+
}).listen(1337, "127.0.0.1");
7+
console.log('Server running at http://127.0.0.1:1337/')

chapter10/代码清单10-1/app.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var http = require('http');
2+
try{
3+
http.createServer(function (req, res) {
4+
if(req.url!=="/favicon.ico"){
5+
noneexist();
6+
res.writeHead(200, {'Content-Type': 'text/html'});
7+
res.write('<head><meta charset="utf-8"/></head>');
8+
res.end('你好\n');
9+
}
10+
}).listen(1337, "127.0.0.1");
11+
}
12+
catch(err){
13+
console.log('接收客户端请求时发生以下错误:');
14+
console.log(err.code);
15+
}
16+
17+
18+
19+
20+
21+
22+

chapter10/代码清单10-10/app.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var domain = require('domain');
2+
var d=domain.create();
3+
d.on('error',function(err){
4+
console.log('Domain对象捕获到错误。');
5+
});
6+
console.log('原始堆栈:');
7+
console.log(domain._stack);
8+
d.run(function() {
9+
console.log('运行domain对象后的堆栈内容:');
10+
console.log(domain._stack);
11+
throw new Error("error");
12+
});
13+
/*d.run(function() {
14+
d.exit();
15+
console.log('运行domain对象后的堆栈内容:');
16+
console.log(domain._stack);
17+
});
18+
d.run(function() {
19+
d.exit();
20+
console.log('运行domain对象后的堆栈内容:');
21+
console.log(domain._stack);
22+
throw new Error("error");
23+
});
24+
25+
d.run(function() {
26+
d.exit();
27+
console.log('运行exit方法后的堆栈内容:');
28+
console.log(domain._stack);
29+
d.enter();
30+
console.log('运行enter方法后的堆栈内容:');
31+
console.log(domain._stack);
32+
throw new Error("error");
33+
});*/
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+

chapter10/代码清单10-11/app.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var domain = require('domain');
2+
var d1=domain.create();
3+
d1.name="d1";
4+
d1.on('error',function(err){
5+
console.log('d1对象捕获到错误。');
6+
});
7+
var d2=domain.create();
8+
d2.name="d2";
9+
d2.on('error',function(err){
10+
console.log('d2对象捕获到错误。');
11+
});
12+
console.log('原始堆栈:');
13+
console.log(domain._stack);
14+
d1.run(function() {
15+
d2.run(function() {
16+
console.log('最终堆栈:');
17+
console.log(domain._stack);
18+
throw new Error("first");
19+
});
20+
});
21+
/*d1.run(function() {
22+
d2.run(function() {
23+
d1.enter();
24+
console.log('最终堆栈:');
25+
console.log(domain._stack);
26+
throw new Error("first");
27+
});
28+
});*/
29+
/*d1.run(function() {
30+
d2.run(function() {
31+
d1.exit();
32+
console.log('最终堆栈:');
33+
console.log(domain._stack);
34+
});
35+
});*/
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+

chapter10/代码清单10-2/app.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var http = require('http');
2+
http.createServer(function (req, res) {
3+
if(req.url!=="/favicon.ico"){
4+
noneexist();
5+
res.writeHead(200, {'Content-Type': 'text/html'});
6+
res.write('<head><meta charset="utf-8"/></head>');
7+
res.end('你好\n');
8+
}
9+
}).listen(1337, "127.0.0.1");
10+
process.on('uncaughtException', function(err) {
11+
console.log('接收客户端请求时发生以下错误:');
12+
console.log(err);
13+
});
14+
15+
16+
17+
18+
19+
20+
21+
22+

chapter10/代码清单10-3/app.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var http = require('http');
2+
var domain = require('domain');
3+
http.createServer(function (req, res) {
4+
var d = domain.create();
5+
6+
d.once('error', function(err) {
7+
res.writeHead(200, {'Content-Type': 'text/html'});
8+
res.write('<head><meta charset="utf-8"/></head>');
9+
res.write('服务器端接收客户端请求时发生以下错误:');
10+
res.end(err.message);
11+
});
12+
d.run(function() {
13+
if(req.url!=="/favicon.ico"){
14+
noneexist();
15+
res.writeHead(200, {'Content-Type': 'text/html'});
16+
res.write('<head><meta charset="utf-8"/></head>');
17+
res.end('你好\n');
18+
}
19+
});
20+
}).listen(1337, "127.0.0.1");
21+
22+
23+
24+
25+
26+
27+
28+
29+

chapter10/代码清单10-4/app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var domain=require('domain');
2+
var fs=require('fs');
3+
var d = domain.create();
4+
d.name='d1';
5+
d.on('error', function(err) {
6+
console.error('%s捕获到错误!',d.name,err);
7+
});
8+
d.run(function() {
9+
process.nextTick(function() {
10+
setTimeout(function() { //模拟一个回调函数
11+
fs.open('non-existent file', 'r', function(err, fd) {
12+
if (err) throw err;
13+
});
14+
}, 1000);
15+
});
16+
});
17+
//d.dispose();
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+

chapter10/代码清单10-5/client.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var http = require('http');
2+
var options = {
3+
hostname: 'localhost',
4+
port: 1337,
5+
path: '/',
6+
method: 'POST'
7+
};
8+
var req = http.request(options,function(res) {
9+
res.setEncoding('utf8');
10+
res.on('data', function (chunk) {
11+
console.log('响应内容: '+chunk);
12+
});
13+
});
14+
req.write('你好。');
15+
req.end('再见。');
16+

chapter10/代码清单10-5/server.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var http = require('http');
2+
var domain = require('domain');
3+
http.createServer(function(req, res) {
4+
var reqd = domain.create();
5+
reqd.add(req);
6+
reqd.add(res);
7+
reqd.on('error', function(err) {
8+
res.writeHead(200);
9+
res.write('服务器端接收客户端请求时发生以下错误:');
10+
res.end(err.message);
11+
});
12+
res.writeHead(200);
13+
//reqd.remove(req);
14+
req.on('data',function(){
15+
noneexists();
16+
res.write('你好。');
17+
res.end();
18+
});
19+
}).listen(1337);
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+

chapter10/代码清单10-7/app.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var fs=require('fs');
2+
var domain = require('domain');
3+
var d = domain.create();
4+
fs.readFile('./test.txt',d.bind(function(err, data) {
5+
if(err) throw err;
6+
else console.log(data);
7+
}));
8+
d.on('error', function(err) {
9+
console.log('读取文件时发生以下错误:');
10+
console.log(err);
11+
});
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+

chapter10/代码清单10-8/app.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var fs=require('fs');
2+
var domain = require('domain');
3+
var d = domain.create();
4+
fs.readFile('./test.txt',d.intercept(function(err, data) {
5+
console.log(data);
6+
}));
7+
d.on('error', function(err) {
8+
console.log('读取文件时发生以下错误:');
9+
console.log(err);
10+
});
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+

chapter10/代码清单10-9/app.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var domain = require('domain');
2+
var d1=domain.create();
3+
d1.name="d1";
4+
var d2=domain.create();
5+
d2.name="d2";
6+
console.log('原始堆栈:');
7+
console.log(domain._stack);
8+
d1.run(function() {
9+
console.log('d1对象:');
10+
console.log(d1);
11+
console.log('运行d1对象后的堆栈内容:');
12+
console.log(domain._stack);
13+
});
14+
d2.run(function() {
15+
console.log('d2对象:');
16+
console.log(d2);
17+
console.log('运行d2对象后的堆栈内容:');
18+
console.log(domain._stack);
19+
});
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+

chapter11/代码清单11-1/app.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var crypto = require('crypto');
2+
var fs = require('fs');
3+
var shasum = crypto.createHash('sha1');
4+
var s = fs.ReadStream('./app.js');
5+
s.on('data', function(d) {
6+
shasum.update(d);
7+
});
8+
s.on('end', function() {
9+
var d = shasum.digest('hex');
10+
console.log(d);
11+
});
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+

chapter11/代码清单11-10/app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var zlib=require('zlib');
2+
var fs=require('fs');
3+
var out=fs.createWriteStream('compress.log');
4+
var input = 'abcdefghijklmnopqrstuvwxyz';
5+
zlib.gzip(input,function(err,buffer) {
6+
if (!err) {
7+
zlib.unzip(buffer,function(err,buffer) {
8+
if (!err) {
9+
console.log(buffer.toString());
10+
out.write(buffer.toString());
11+
}
12+
});
13+
}
14+
});
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+

0 commit comments

Comments
 (0)