HTTP全称是超文本传输协议,构建于TCP之上,属于应用层协议。
let server = http.createServer([requestListener]);
server.on('request',requestListener);
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('服务器端开始监听!')});
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();
});
server.on('error',function(){
if(e.code == 'EADDRINUSE'){
console.log('端口号已经被占用!);
}
});
let server = http.createServer(function(req,res){
});
server.on('connection',function(){
console.log(客户端连接已经建立);
});
设置超时时间,超时后不可再复用已经建立的连接,需要发请求需要重新建立连接。默认超时时间时2分钟
server.setTimeout(msecs,callback);
server.on('timeout',function(){
console.log('连接已经超时');
});
let http = require('http');
let fs = require('fs');
let server = http.createServer(function(req,res){
if(req.url != '/favicon.ico'){
let out = fs.createWriteStream(path.join(__dirname,'request.log'));
out.write('method='+req.method);
out.write('url='+req.url);
out.write('headers='+JSON.stringify(req.headers));
out.write('httpVersion='+req.httpVersion);
}
}).listen(8080,'127.0.0.1);
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);
querystring模块用来转换URL字符串和URL中的查询字符串
querystring.parse(str,[sep],[eq],[options]);
querystring.stringify(obj,[sep],[eq]);
url.parse(urlStr,[parseQueryString]);
?
#
http.ServerResponse对象表示响应对象
response.writeHead(statusCode,[reasonPhrase],[headers]);
设置、获取和删除Header
response.setHeader('Content-Type','text/html;charset=utf-8');
response.getHeader('Content-Type');
response.removeHeader('Content-Type');
response.headersSent 判断响应头是否已经发送
判断响应头是否已经发送
let http = require('http');
let server = http.createServer(function(req,res){
console.log(resopnse.headersSent?"响应头已经发送":"响应头未发送!");
res.writeHead(200,'ok);
console.log(resopnse.headersSent?"响应头已经发送":"响应头未发送!");
});
不发送Date
res.sendDate = false;
可以使用write方法发送响应内容
response.write(chunk,[encoding]);
response.end([chunk],[encoding]);
可以使用setTimeout方法设置响应让超时时间,如果在指定时间内不响应,则触发timeout事件
response.setTimeout(msecs,[callback]);
response.on('timeout',callback);
在响应对象的end方法被调用之前,如果连接中断,将触发http.ServerResponse对象的close事件
response.on('close',callback);
net
onconnection
_http_server.js
连接监听
connectionListenerInternal
socketOnData
onParserExecuteCommon
parserOnIncoming
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();
可以使用abort方法来终止本次请求
req.abort();
如果请求过程中出错了,会触发error事件
request.on('error',function(err){});
建立连接过程中,为该连接分配端口时,触发socket
事件
req.on('socket',function(socket){
socket.setTimeout(1000);
socket.on('timeout',function(){req.abort()});
});
可以使用get方法向服务器发送数据
http.get(options,callback);
可以使用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();
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);