Node的Request对象
在向服务器发送请求时,Node会帮助我们封装Request对象,Request对象携带了客户端向服务端传递过来的所有信息,如本次请求的URL、请求方式Method与请求的headers等信息。
const http = require('http') const server = http.createServer((req, res) => { // request 对象 console.log(req.url) console.log(req.method) console.log(req.headers) res.end('Hello world!') }) server.listen(8888, () => { console.log('服务已启动!') })
Request对象中url的处理
客户端在发送请求时,会请求不同的数据,而服务端根据不同的请求地址,作出对应的响应。
const http = require('http') const server = http.createServer((req, res) => { // 请求地址 // http://localhost:8888/login // http://localhost:8888/home if(req.url === '/login') { res.end('This is the landing page') } else if(req.url === '/home') { res.end('This is the home page') } }) server.listen(8888, () => { console.log('服务已启动!') })
看上面的代码块我们知道,如果地址后面需要携带参数,上面的请求地址是得不到响应的,这时就需要借助内置模块url获取pathname与内置模块querystring解析字符串。
const http = require('http') const url = require('url') const qs = require('querystring') const server = http.createServer((req, res) => { // 请求地址 // http://localhost:8888/login?name=liming&password=123456; // url -> /login?name=liming&password=123456 // console.log(url.parse(req.url)) // 通过打印 url.parse(req.url) 知其是个对象,包含pathname、query等字段 // pathname: '/loagin', query: 'name=liming&password=123456' const {pathname, query} = url.parse(req.url); if(pathname === '/login') { // console.log(query) // 通过打印 query 得到的是字符串 'name=liming&password=123456' // 通过 querystring 模块把 query 字符串转为对象 const {name, password} = qs.parse(query) console.log(name, password) res.end('获取地址携带参数') } }) server.listen(8888, () => { console.log('服务已启动!') })
Request对象中method的处理
除了上面明文的请求方式外,还可以把参数放在body中请求,我们可以通过Request对象的method来获取请求方式,从而进行对应的处理,以post为例:
const http = require('http') const url = require('url') const qs = require('querystring') const server = http.createServer((req, res) => { const { pathname } = url.parse(req.url); if(pathname === '/login') { if(req.method === 'POST') { // 拿到body请求的数据 原生 request 对象中是没有body的 // body 中的数据是通过流的方式写入的,所以需要监听 data事件 // 当 data事件获取输入流时调用回调函数并把数据传入 req.setEncoding('utf-8') req.on('data', (data) => { // 通过打印 data 知道是以二进制传过来的,通过下面方法获取对应字符串。 // data.toString() 转对应字符串 // req.setEncoding('utf-8') 提前设置请求的数据编码格式 // 当是字符串对象,可用JSON.parse(stringObj)方法转对象 console.log(data) }) res.end('获取body中的数据!') } } }) server.listen(8888, () => { console.log('服务已启动!') })
Request对象中headers的处理
Request对象的headers中也包含很多有用的信息,客户端会默认传递过来一些信息:
// 打印 req.headers { 'content-type': 'application/json', 'user-agent': 'PostmanRuntime/7.28.2', accept: '*/*', 'postman-token': 'bf648d73-83e6-499c-b8f0-904460035aba', host: 'localhost:8888', 'accept-encoding': 'gzip, deflate, br', connection: 'keep-alive', 'content-length': '51' }
content-type请求携带的数据的类型:
- application/json表示是一个json类型;
- text/plain表示是文本类型;
- application/xml表示是xml类型;
- multipart/form-data表示是上传文件;
user-agent:客户端相关的信息;
accept:告知服务器,客户端可接受文件的格式类型;
accept-encoding:告知服务器,客户端支持的文件压缩格式,比如js文件可以使用gzip编码,对应.gz文件;
connection: http是基于TCP协议的,通常在进行一次请求和响应结束后会立刻中断:
- 所有连接默认是connection: keep-alive的;
- 不同的Web服务器会有不同的保持keep-alive的时间;
- Node中默认是5s中;
content-length:文件的大小或者长度
后续
Node的Response对象