2021/7/22
前面学习了get请求参数,今天补上没看的post参数吧
一、post 请求参数
- 参数被放置在请求体中(也就是报文中)进行传输
- 获取POST参数需要使用data事件和end事件
- 使用querystring系统模块将参数转换为对象格式
//用于创建网站服务器的模块
const http = require('http');
//app对象就是网站服务器对象
const app = http.createServer();
//系统模块将参数转换为对象格式
const querystring = require('querystring');
// 当客户端发送请求的时候
app.on('request', (req, res) => {
//post参数是通过事件的方式接收的
//data 当请求参数传递的时候发出data事件
//end 当参数传递完成的时候发出end事件
let postParams = '';
//监听参数传输事件
req.on('data', params => {
postParams += params;
});
//监听参数传输完毕事件
req.on('end', () => {
console.log(querystring.parse(postParams)