1. HTTP服务器 #

HTTP全称是超文本传输协议,构建于TCP之上,属于应用层协议。

1.1 创建HTTP服务器 #

let server  = http.createServer([requestListener]);
server.on('request',requestListener);

1.2 启动HTTP服务器 #

server.listen(port,[host],[backlog],[callback]);
server.on('listening',callback);
let http = require('http');
let server = http.createServer(function(req,res){
}).listen(8080,'127.0.0.1',function(){console.log('服务器端开始监听!')});

1.3 关闭HTTP服务器 #

server.close();
server.on('close',function(){});
let http = require('http');
let server = http.createServer(function(req,res){
});
server.on('close',function(){
    console.log('服务器关闭');
});
server.listen(8080,'127.0.0.1',function(){
    console.log('服务器端开始监听!')
    server.close();
});

1.4 监听服务器错误 #

server.on('error',function(){
    if(e.code == 'EADDRINUSE'){
         console.log('端口号已经被占用!);   
    }
});

1.5 connection #

let server = http.createServer(function(req,res){
});
server.on('connection',function(){
    console.log(客户端连接已经建立);
});

1.6 setTimeout #

设置超时时间,超时后不可再复用已经建立的连接,需要发请求需要重新建立连接。默认超时时间时2分钟

server.setTimeout(msecs,callback);
server.on('timeout',function(){
    console.log('连接已经超时');
});

1.7 获取客户端请求信息 #

let http = require('http');
let fs = require('fs');
let server = http.createServer(function(req,res){
  let body = [];
  req.on('data',function(data){
    body.push(data);
  });
  req.on('end',function(){
      let result = Buffer.concat(body);
      console.log(result.toString());
  });
}).listen(8080,'127.0.0.1);

1.8 querystring #

querystring模块用来转换URL字符串和URL中的查询字符串

1.8.1 parse方法用来把字符串转换成对象 #

querystring.parse(str,[sep],[eq],[options]);

1.8.2 stringify方法用来把对象转换成字符串 #

querystring.stringify(obj,[sep],[eq]);

1.9 querystring #

url.parse(urlStr,[parseQueryString]);

1.10 发送服务器响应流 #

http.ServerResponse对象表示响应对象

1.10.1 writeHead #

response.writeHead(statusCode,[reasonPhrase],[headers]);

1.10.2 Header #

设置、获取和删除Header

response.setHeader('Content-Type','text/html;charset=utf-8');
response.getHeader('Content-Type');
response.removeHeader('Content-Type');
response.headersSent 判断响应头是否已经发送

1.10.3 headersSent #

判断响应头是否已经发送

let http = require('http');
let server = http.createServer(function(req,res){
  console.log(resopnse.headersSent?"响应头已经发送":"响应头未发送!");
  res.writeHead(200,'ok);
  console.log(resopnse.headersSent?"响应头已经发送":"响应头未发送!");
});

1.10.4 sendDate #

不发送Date

res.sendDate = false;

1.10.5 write #

可以使用write方法发送响应内容

response.write(chunk,[encoding]);
response.end([chunk],[encoding]);

1.10.6 timeout #

可以使用setTimeout方法设置响应让超时时间,如果在指定时间内不响应,则触发timeout事件

response.setTimeout(msecs,[callback]);
response.on('timeout',callback);

1.10.7 close #

在响应对象的end方法被调用之前,如果连接中断,将触发http.ServerResponse对象的close事件

response.on('close',callback);

1.10.8 parser #

net
onconnection

_http_server.js
连接监听
connectionListenerInternal
socketOnData
onParserExecuteCommon
parserOnIncoming


2. HTTP客户端 #

2.1 向其他网站请求数据 #

let req = http.request(options,callback);
req.on('request',callback);
request.write(chunk,[encoding]);
request.end([chunk],[encoding]);

let http = require('http');
let options = {
    hostname: 'localhost',
    port: 8080,
    path: '/',
    method: 'GET'
}
let req = http.request(options, function (res) {
    console.log('状态吗:' + res.statusCode);
    console.log('响应头:' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('响应内容', chunk);
    });
});
req.end();

2.2 取消请求 #

可以使用abort方法来终止本次请求

req.abort();

2.3 监听error事件 #

如果请求过程中出错了,会触发error事件

request.on('error',function(err){});

2.4 socket #

建立连接过程中,为该连接分配端口时,触发socket事件

req.on('socket',function(socket){
  socket.setTimeout(1000);
  socket.on('timeout',function(){req.abort()});
});

2.5 get #

可以使用get方法向服务器发送数据

http.get(options,callback);

2.6 addTrailers #

可以使用response对象的addTrailers方法在服务器响应尾部追加一个头信息

let http = require('http');
let path = require('path');
let crypto = require('crypto');


let server = http.createServer(function (req, res) {
    res.writeHead(200, {
        'Transfer-Encoding': 'chunked',
        'Trailer': 'Content-MD5'
    });
    let rs = require('fs').createReadStream(path.join(__dirname, 'msg.txt'), {
        highWaterMark: 2
    });
    let md5 = crypto.createHash('md5');
    rs.on('data', function (data) {
        console.log(data);
        res.write(data);
        md5.update(data);
    });
    rs.on('end', function () {
        res.addTrailers({
            'Content-MD5': md5.digest('hex')
        });
        res.end();
    });
}).listen(8080);
let http = require('http');
let options = {
    hostname: 'localhost',
    port: 8080,
    path: '/',
    method: 'GET'
}
let req = http.request(options, function (res) {
    console.log('状态吗:' + res.statusCode);
    console.log('响应头:' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('响应内容', chunk);
    });
    res.on('end', function () {
        console.log('trailer', res.trailers);
    });
});
req.end();

2.7 制作代理服务器 #

let http = require('http');
let url = require('url');
let server = http.createServer(function (request, response) {
    let {
        path
    } = url.parse(request.url);
    let options = {
        host: 'localhost',
        port: 9090,
        path: path,
        headers: request.headers
    }
    let req = http.get(options, function (res) {
        console.log(res);
        response.writeHead(res.statusCode, res.headers);
        res.pipe(response);
    });
    req.on('error', function (err) {
        console.log(err);
    });
    request.pipe(req);
}).listen(8080);