请求与响应模型
如何发送请求?
- 用 Chrome 地址栏
- 用 curl 命令
- 发送请求的工具叫做 【用户代理】,英文名 User Agent
如何做出一个响应?
node.js 的 http 模块可以做到
console.log('有个傻子发请求过来啦!路径(带查询参数)为:' + pathWithQuery)
if (path === '/') {
response.statusCode = 200
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.write(`
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/x">
</head>
<script src="/y"></script>
<body>
<h1>哈哈哈</h1>
</body>
`)
response.end()
} else if (path === '/x') {
response.statusCode = 200
response.setHeader('Content-Type', 'text/css;charset=utf-8')
response.write(`body{color: red;\n}`)
response.end()
}
else if (path === '/y') {
response.statusCode = 200
response.setHeader('Content-Type', 'text/javascript;charset=utf-8')
response.write(`这是js文件\n`)
response.end()
}
else {
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.write(`你输入的路径不存在对应的内容`)
response.end()
}
注意事项
- 这些代码就是服务器代码,一般放在服务器上
- path是不带查询参数的路径/x
- query是查询参数的对象形式{a:'1'}
- queryString是查询参数的字符串形式?a=1
- pathWithQuery是带查询参数的路径,一般不用
- request是请求对象
- response是响应对象
语法
- `这种字符串`里面可以回车
- '这种字符串'里面要回车只能用 \n 表示
逻辑
- 每次收到请求都会把中间的代码执行一遍
- 用 if else 判断路径,并返回响应
- 如果是已知路径,一律返回200
- 如果是未知路径,一律返回404
- Content-Type 表示内容的「类型/语法」
- response.write() 可以填写返回的内容
- response.end() 表示响应可以发给用户了
注意事项
- URL 里的后缀卵用没有,/y.css 不一定是 CSS 内容
- Content-Type 才是决定文件类型的关键
HTTP 基础概念
请求
- 请求动词 路径加查询参数 协议名/版本
- Host: 域名或 IP
- Accept: text/ html
- Content-Type:请求体的格式
- 回车
- 请求体(也就是上传内容)
细节
- 三部分:请求行、请求头、请求体
- 请求动词有 GET(获取) / POST(上传) / PUT / PATCH / DELETE 等
- 请求体在 GET 请求中一般为空
- 文档位于 RFC 2612 第五章
- 大小写不敏感
响应
- 协议名 / 版本 状态码 状态字符串
- Content-Type: 响应体的格式
- 回车
- 响应体(也就是下载内容)
细节
- 三部分: 状态行、响应头、响应体
- 常见的状态码是考点
- 文档位于RFC 2612第六章
用 curl 构造请求
curl -v http://127.0.0.1:8888/
* Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Date: Thu, 24 Jun 2021 11:47:58 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
<
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/x">
</head>
<script src="/y"></script>
<body>
<h1>哈哈哈</h1>
</body>
* Connection #0 to host 127.0.0.1 left intact
设置请求动词
- -X POST
- 注意大小写
curl -v -X -POST 127.0.0.1:8888/
* Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> -POST / HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: close
<
* Closing connection 0
设置路径和查询参数
- 直接在 URL 后面加
- 锚点是不会发送到服务器的
curl -v -X -POST 127.0.0.1:8888/xxxx?wd=hi#hello
* Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> -POST /xxxx?wd=hi HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.65.3
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: close
<
* Closing connection 0
设置请求头
- -H 'name:value' 或者 --header 'name:value'
curl -v -X -POST -H'text:html' 127.0.0.1:8888/xxxx?wd=hi#hello
* Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> -POST /xxxx?wd=hi HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.65.3
> Accept: */*
> text:html
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: close
<
* Closing connection 0
设置请求体
- -d '内容' 或者 --data '内容'
curl -v -X -POST -H 'Content-Type: text/plain;charset-utf-8' -d '请求体内容' 127.0.0.1:8888/
* Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#0)
> -POST / HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.65.3
> Accept: */*
> Content-Type: text/plain;charset-utf-8
> Content-Length: 10
>
* upload completely sent off: 10 out of 10 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: close
<
* Closing connection 0
用 Node.js 读取请求
读取请求动词
- request.method
JS
var method = request.method
console.log('method: ');
console.log(method);
BASH
curl http://127.0.0.1:8888/
curl -X POST http://127.0.0.1:8888/
method:
GET | POST
读取路径
- request.url 路径,带查询参数
JS var pathWithQuery = request.url console.log('有个傻子发请求过来啦!路径(带查询参数)为:' + pathWithQuery); BASH curl http://127.0.0.1:8888/?=hahaha=hello 有个傻子发请求过来啦!路径(带查询参数)为:/?=hahaha=hello
- path 纯路径,不带查询参数
- query 只有查询参数
读取请求头
- request.headers['Accept']
JS console.log(request.headers); BASH curl -X POST -H 'simpson:hello' http://127.0.0.1:8888/?=hahaha=hello host: '127.0.0.1:8888', 'user-agent': 'curl/7.65.3', accept: '*/*', simpson: 'hello'
用 node.js 设置响应
- 设置响应状态码
response.statusCode = 200
- 设置响应头
response.setHeader('Content-Type', 'text/html;charset=utf-8')
- 设置响应体
response.write('内容') //可追加内容
*本文为鲲游北冥的原创文章,著作权归本人和饥人谷所有,转载务必注明来源