一:处理请求的 查询字符串;
//引入querystring模块
const queryString = require('querystring');
//引入 url 模块
const URL = require('url');
const qs='a=111&b=222';
//使用querystring解析查询字符串;
console.log(queryString.parse(qs)); //{ a: '111', b: '222' }
const us='http://www.baidu.com/index.html?a=111&b=222';
console.log(URL.parse(us));
// 得到 对象 但是 第二个参数没有传,query没有解析成对象
// {
// protocol: 'http:',
// slashes: true,
// auth: null,
// host: 'www.baidu.com',
// port: null,
// hostname: 'www.baidu.com',
// hash: null,
// search: '?a=111&b=222',
// query: 'a=111&b=222',
// pathname: '/index.html',
// path: '/index.html?a=111&b=222',
// href: 'http://www.baidu.com/index.html?a=111&b=222' }
console.log( URL.parse(us,true));
//得到 对象 ,第二个参数传true,query解析成对象
// {
// protocol: 'http:',
// slashes: true,
// auth: null,
// host: 'www.baidu.com',
// port: null,
// hostname: 'www.baidu.com',
// hash: null,
// search: '?a=111&b=222',
// query: { a: '111', b: '222' },
// pathname: '/index.html',
// path: '/index.html?a=111&b=222',
// href: 'http://www.baidu.com/index.html?a=111&b=222' }
2, 关于node 的post的请求 原生写法;
const http = require('http');
const server=http.createServer((req,res)=>{
//定义接受数据的空串
let str="";
// 一般 post的请求 会 一段一段的传过来;我们需要一段一段的接受过来
// res.on 的data方法 就是接受 一段一段的数据的;数据多的话 会触发很多次
res.on('data',(err,data)=>{
str+=data;
});
// res.on 的end方法就是最后结尾的标志; 只会触发一次 ;
res.on('end',(err,data)=>{
console.log(str)
})
});
server.listen(8888)