HTTP(Ⅸ)—— 数据协商

在客户端向服务器发送请求的时候,会告诉服务器我想要拿到的数据格式,以及数据相关的限制,服务端会根据他的请求,做出判断,然后决定要返回什么样子的数据。这就是数据协商上。

请求的Accept

  • Accept:表示的是我想要什么类型的数据
  • Accept-Encoding:表示我想要数据的编码方式
  • Accept-Language:返回数据数据语言
  • User-Agent:表示浏览器的相关信息,来判断是判断返回是PC端页面还是移动端页面。

返回的通过Connect

  • Content-Type:实际返回的数据格式
  • Content-Encoding:服务端数据压缩格式的声明
  • Content-Language:是否通过你的请求返回了相关的语言

数据压缩,首先来看图:

我们将server.js修改为如下:

const http = require('http')
const fs = require('fs')
const zlib = require('zlib')
http.createServer(function(request, response) {
    console.log('request come', request.url)
    const html = fs.readFileSync('test.html')
    response.writeHead(200, {
        'Content-Type': 'text/html',
        'COntent-Encoding': 'gzip'
    })
     response.end(zlib.gzipSync(html))
}).listen(8888)
console.log('server listening on 8888')

此时重启服务刷新,我们会看见如下信息:

相对于上次的图,我们可以看到在传输过程中的文件内容变小了。

猜你喜欢

转载自blog.csdn.net/zhanghuali0210/article/details/82085555