简单了解 node http 模块javascript
文章记录了对http 模块的简单使用与理解。html
先写个小例子java
服务端:node
let http = require('http')
let server = http.createServer((req, res) => {
let buf = []
req.on('data', data => {
buf.push(data)
})
req.on('end', () => {
let str = Buffer.concat(buf).toString()
res.end(str)
})
})
server.listen(8080, () => {
console.log('server start')
})复制代码
请求报文由如下组成api
node http 模块 在TCP链接的读操做上,将数据解析成(以空行分割,报文头和报文体):缓存
报文头部分:
req.method
req.httpVersionMajor
req.httpVersionMinor
req.httpVersion
req.upgrade
...
req.headers = {
Content-Length: 15
Content-Type: application/x-www-form-urlencoded
... ...
}
//
报文主体部分:
一个可读流对象req,能够继续报文主体数据的读取复制代码
服务端经过http.ServerResponse 实例,来向客户端(数据请求方)返回本次请求数据。包括响应头,响应体(内部经过socket来发送信息)。服务器
//中间有个空行
"HTTP/1.1 200 OK
Date: Sat, 30 Nov 2019 05:10:05 GMT
Connection: close
Content-Length: 25
"
+ 请求体部分数据或所有数据复制代码
经过的方法app
// 具体使用参考文档
// 设置头部,头部并无发送
response.setHeader(name, value)
// 设置头部,向请求发送响应头,此方法只能在消息上调用一次,而且必须在调用 response.end() 以前调用。
response.writeHead()
// 若是调用此方法而且还没有调用 response.writeHead(),则将切换到隐式响应头模式并刷新隐式响应头。
// 这会发送一块响应主体。 能够屡次调用该方法以提供连续的响应主体片断。
response.write()
// 此方法向服务器发出信号,代表已发送全部响应头和主体,该服务器应该视为此消息已完成。 必须在每一个响应上调用此 response.end() 方法。
response.end() 复制代码
let http = require('http')
let options = {
host: 'localhost',
port: 8080,
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}
// 请求并无 发出 req 是个可写流
let req = http.request(options)
req.on('response', res => {
console.log(res.headers)
let buf = []
res.on('data', data => {
buf.push(data)
})
res.on('end', () => {
console.log(Buffer.concat(buf).toString())
})
})
// write 向请求体写数据
req.write('name=luoxiaobu&title=http')
// 实际的请求头将会与第一个数据块一块儿发送,或者当调用 request.end() 时发送。
req.end()复制代码
大概代码执行流程:socket
http.request() 返回 http.ClientRequest 类的实例。http.ClientRequest 内部建立了一个socket来发起请求。函数
ClientRequest 实例是能够看作可写流。若是须要使用 POST 请求上传文件,则写入到 ClientRequest 对象。
response事件 每次服务器端有数据返回响应时都会触发。
http.IncomingMessage 对象,它可用于访问本次服务器端返回的,响应状态、消息头、以及数据。
http客户端:在TCP模块套接字 socket 上,将求报文头和报文主体组装成数据发送出去,将接受的数据解析出响应报文头和报文主体,。
//中间有个空行
"POST / HTTP/1.1
content-type: application/x-www-form-urlencoded
Host: localhost:8080
Connection: close
Transfer-Encoding: chunked
"
+ 请求体部分数据或所有数据复制代码
经过的方法
// 具体使用参考文档
const postData = querystring.stringify({
'msg': '你好世界'
});
const options = {
hostname: 'localhost:8080',
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
// 内部会处理 options 解析出头部
const req = http.request(options, (res) => {
});
// 这会发送一块响应主体。 能够屡次调用该方法以提供连续的响应主体片断。
response.write()
// 此方法向服务器发出信号,代表已发送全部响应头和主体,该服务器应该视为此消息已完成。 必须在每一个响应上调用此 response.end() 方法。
response.end() 复制代码
响应报文图:
响应报文由如下组成
node http 模块 在TCP链接的读操做上,将数据解析成:
报文头部分:
res.statusCode = statusCode;
res.statusMessage = statusMessage;
res.httpVersionMajor
res.httpVersionMinor
res.httpVersion
res.upgrade
...
res.headers = {
Content-Length: 15
Content-Type: application/x-www-form-urlencoded
... ...
}
//
报文主体部分:
一个可读流对象,能够继续报文主体数据的读取复制代码
文章记录了对http 模块的简单使用与理解,要深刻理解http 模块,还需多看文档,代码实践。
文中例子比较粗糙,理解不许确之处,还请教正。
参考资料:<<图解HTTP>>