Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node学习与实践增加注释,网络服务部分之50% #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/2016.11.08-node-http/.DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion examples/2016.11.08-node-http/chunkRes/html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var http = require('http');

// 如果Content-Type是 text/html 则访问页面显示hello , 两秒后又被附加 world
// 如果Content-Type是 text/html 则访问页面会延迟两秒才打开,直接显示 hello world
// 而且 setTimeout里的 res.en()语句不能放在外面,否则会报错 Error: write after end
http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.write('hello');
Expand All @@ -8,5 +11,5 @@ http.createServer(function(req, res) {
res.write(' world!');
res.end();
}, 3000);

}).listen(3000);
12 changes: 11 additions & 1 deletion examples/2016.11.08-node-http/client/eventResponse.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
var http = require('http');

var url = 'http://id.qq.com/';
// var url = 'http://id.qq.com/';
var url = 'http://127.0.0.1:3000';

var server = http.createServer(function(req, res) {
var url = req.url;
res.writeHead(200, {
'Content-Type': 'text/html;charset=utf-8'
})
res.end("您要访问的地址是"+url);
})
server.listen(3000);
var client = http.get(url, function(res){
res.pipe(process.stdout);
console.log('1. response event');
});

Expand Down
3 changes: 2 additions & 1 deletion examples/2016.11.08-node-http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ var http = require('http');
var server = http.createServer(function(serverReq, serverRes){
serverRes.end('hello');
});

// 会在本地3000端口输出hello
server.listen(3000);

// 有下面这个才会在vim命令执行后紧接着输出上面的 hello 返回语句
var client = http.get('http://127.0.0.1:3000', function(clientRes){
clientRes.pipe(process.stdout);
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// 运行不了 !!!!!!!

var qs = require("querystring");
var http = require("http");

var options = {
"method": "CONNECT",
"hostname": "127.0.0.1",
"port": "3000",
"port": "4000",
"path": "/",
"headers": {
"content-type": "application/x-www-form-urlencoded",
Expand Down
1 change: 1 addition & 0 deletions examples/2016.11.08-node-http/httpServerEventError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var http = require('http');
var PORT = 3000;
var noop = function(){};

// 这个例子是在监测服务端口占用问题
var svr = http.createServer(noop);
var anotherSvr = http.createServer(noop);

Expand Down
Binary file not shown.
15 changes: 10 additions & 5 deletions examples/2016.11.08-node-http/serverReq/eventAbortedClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
var http = require('http');

// 客户端表现
// abort请求时,res.pipe(process.stdout) 这行代码是否添加,会影响close是否触发。
// 没有res.pipe(process.stdout):close不触发。
// 有res.pipe(process.stdout):close触发。
var server = http.createServer(function(req, res){

console.log('1、服务端:收到客户端请求');
Expand All @@ -16,8 +20,8 @@ server.listen(3000, function(){
var client = http.get('http://127.0.0.1:3000/aborted', function(res){

console.log('2、客户端:收到服务端响应');

// res.pipe(process.stdout); 注意这行代码
// 注意这行代码是否添加,会影响close是否触发。
res.pipe(process.stdout);

res.on('aborted', function(){
console.log('3、客户端:aborted触发!');
Expand All @@ -30,6 +34,7 @@ server.listen(3000, function(){
});

// 输出如下
// 1、收到客户端请求: /aborted
// 2、客户端请求aborted
// 3、客户端请求close
// 1、服务端:收到客户端请求
// 2、客户端:收到服务端响应
// 3、客户端:aborted触发!
// 4、客户端:close触发!
3 changes: 3 additions & 0 deletions examples/2016.11.08-node-http/serverReq/eventAbortedServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var http = require('http');

// 服务端表现
// abort请求时,服务端req的aborted、close事件都会触发;(诡异)
// 请求正常完成时,服务端req的close事件不会触发;(也很诡异)
var server = http.createServer(function(req, res){

console.log('1、收到客户端请求: ' + req.url);
Expand Down
7 changes: 6 additions & 1 deletion examples/2016.11.08-node-http/serverReq/getClientPostBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ var http = require('http');
var url = require('url');
var querystring = require('querystring');

// 访问http://localhost:3000/页面上有 ok , 命令行上 post body is: ,
// 也就是说访问页面是get请求,只会触发 end 不会触发 data
// 通过curl构造post请求:curl -d 'nick=casper&hello=world' http://127.0.0.1:3000
// 此时命令行上又打印出一行 post body is: nick=casper&hello=world
var server = http.createServer(function(req, res){

var body = '';
var body = '';
// 实时监控的,每次只要发起post请求就会触发 data -> end
req.on('data', function(thunk){
body += thunk;
});
Expand Down
10 changes: 8 additions & 2 deletions examples/2016.11.08-node-http/serverReq/reqDestroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ server.listen(3000, function(){
var client = http.get('http://127.0.0.1:3000/aborted', function(res){
// do nothing
});

// 此例之所以会触发 client error 是因为 createServer里没有给 res.end(),
// 若给了,即时req中destroy也不会触发 client error
client.on('error', function(error){
console.log('客户端:client error触发!' + error.message);
});
});
});

// 控制台输出如下:
// 服务端:收到客户端请求
// 服务端:req socket error: 测试destroy
// 客户端:client error触发!socket hang up
27 changes: 24 additions & 3 deletions examples/2016.11.08-node-http/serverRes/basic.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
var http = require('http');

// 1, 这里如果 res.send("xxx")会报错 : res.send is not a function , 为什么呢???
// 2, 想要设置200/ok,可以 res.writeHead(200, 'ok');
// 也可以 res.statusCode = 200; res.statusMessage = 'ok';
// 3, 已经通过 res.setHeader() 设置了header,
// 当通过 res.writeHead() 设置同名header,res.writeHead() 的设置会覆盖之前的设置。
// 但 writeHeader之后就不能再用setHeader, 否则报错 Error: Can't set headers after they are sent.。
// 4,响应头部操作
// // 增
// res.setHeader('Content-Type', 'text/plain');
// // 删
// res.removeHeader('Content-Type');
// // 改
// res.setHeader('Content-Type', 'text/plain');
// res.setHeader('Content-Type', 'text/html'); // 覆盖
// // 查
// res.getHeader('content-type');
// 其中略显不同的是 res.getHeader(name),name 用的是小写,但返回值没做特殊处理。
var server = http.createServer(function(req, res){

// res.setHeader('Content-Type', 'text/html');
res.writeHead(200, 'ok', {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Type': 'text/html; charset=utf-8',
// 'X-Content-Type-Options': 'nosniff'
// 'Connection': 'Transfer-Encoding',
// 'Transfer-Encoding': 'chunked'
});

res.write('hello');
// res.setTimeout(2000);
// res.write('world');

setTimeout(function(){
res.end()
res.write('world');
res.end();
}, 2000)
// res.end();
// res.send("ok")

});

server.listen(3000);
20 changes: 11 additions & 9 deletions examples/2016.11.08-node-http/serverRes/setTimeout.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
var http = require('http');

// 在命令号执行此文件后,会触发两次 = =b1,一次是因为server里主动一次,另一次是因为下面client语句又去请求一次
// 而 b3 , b4都不会触发
var server = http.createServer(function(req, res){
res.setTimeout(1000);
// res.setTimeout(1000, function(socket){
// console.log(typeof socket);
// console.log('= =b')
// });
// res.setTimeout(1000);
res.setTimeout(1000, function(socket){
console.log(typeof socket);
console.log('= =b1')
});
});

// server.on('timeout', function(socket){
// // console.log(typeof socket);
// // console.log('= =b')
// console.log(typeof socket);
// console.log('= =b2')
// });

server.listen(3000);

var client = http.get('http://127.0.0.1:3000', function(res){
res.on('timeout', function(socket){
console.log(typeof socket);
console.log('= =b')
console.log('= =b3')
});
});

client.on('timeout', function(socket){
console.log(typeof socket);
console.log('= =b')
console.log('= =b4')
});