http模块 如何获取http请求的报文?

一、获取http请求的报文语法总结

要想获取请求数据,需要通过request对象

(1)获取请求方法 request.method

(2)获取请求版本 request.httpVersion

(3)获取路径 request.url

(4)获取url路径 request('url').parse(request.url).pathname

(5)获取url查询字符串 request.('url').parse(request.url,true).query

(6)获取请求头 request.headers

(7)获取请求体 request.on('data',function(chunk){}) <br/> request.on('end',function(){}); 声明一个变量data,绑定data事件与end事件,最后添加响应

注意:

(1)request.url只能获取路径以及查询字符串,无法获取url中的域名以及协议内容

(2)favicon.ico 是属于浏览器自动发送的请求

二、代码示例:

(1)获取请求、请求的url、http协议的版本号、http请求头、http请求体
javascript 复制代码
// 1.导入http模块
const http=require('http');
// 2.创建服务对象
const server=http.createServer((request,response)=>{
    response.end('hello,world!'); //设置响应体
    // 获取请求的方法:
    console.log(request.method);  //GET GET  我们浏览器有两个名称项目,所以发了两次get请求
    // 获取请求的url:
    // 它只包含url中的路径与查询字符串
    console.log(request.url);  //  /search?keyword=5&num=1  /favicon.ico
    // 获取http协议的版本号:
    console.log(request.httpVersion); //1.1  1.1
    // 获取http的请求头:
    console.log(request.headers); //结果是一个对象,对象里面包含了请求头的所有内容
    // 获取其中一个请求头例如host,我们可以console.log(request.headers.host); 

    // 获取http的请求体内容:***
    // (1)声明一个变量
    let body='';
    // (2)绑定data事件
    request.on('data',chunk=>{
        body+=chunk;
    });
    // (3)绑定end事件
    request.on('end',()=>{
    console.log(body);
    // 添加响应:
    response.end('Hello HTTP!');
    }); //回车后控制台结果为空,因为我们发送的是一个get请求。
    // 解决办法:借助于上文我们那个html里面写的那个提交的表单来发送一个post请求
    //提交后发送请求,控制台结果为我们填写的请求体内容:username=111&password=111

});
// 3.监听端口,启动服务
server.listen(9000,()=>{
    console.log('服务已经启动...');
});
//注意启动谷歌端口时输入http://127.0.0.1:9000/search?keyword=111; 才能查询到url路径为search字符串为111
(2)获取http请求报文中的请求路径与查询字符串:

虽然request.url已包含属性和查询字符串,但使用不便,若只需其中一个不好提取,于是用到了如下路径和字符串的单独查询方法:

方法一:

javascript 复制代码
const http=require('http');
// 一、获取路径:
// 1.导入url模块
const url=require('url');

const server=http.createServer((request,response)=>{
    response.end('url');
    // 2.解析request.url
    console.log(request.url); //虽然request.url已包含属性和查询字符串 /search?keyword=5&num=1 /favicon.ico  但使用不便,若只需其中一个不好提取,于是用到了如下路径和字符串的单独查询方法
    let res=url.parse(request.url,true); //使用parse去解析request.url里的内容
    console.log(res); //输出可以看到 路径为pathname: '/search', pathname: '/favicon.ico',  后接true,使查询字符串变成了一个对象
    let pathname=res.pathname;
    console.log(pathname); //  /search  /favicon.ico 可以看到pathname里就是我们所要的路径

// 二、获取查询字符串:
let keyword=res.query.keyword;
console.log(keyword); //5 undefined  第一个url的keyword为5,第二个url里面没有包含keyword这个参数所以为undefined

});
server.listen(9000,()=>{ 
    console.log('服务已启动...');
});
//注意启动谷歌端口时输入http://127.0.0.1:9000/search?keyword=111; 才能查询到url路径为search字符串为111

方法二:

javascript 复制代码
const http=require('http');

const server=http.createServer((request,response)=>{
    response.end('url new');
// 一、获取路径:
// (1)实例化一个URL对象 必须由域名或ip、路径与字符串组成
let url=new URL('http://www.xxx.com/search?a=100&b=200'); //或者new URL('http://127.0.0.1:9000','search?a=100&b=200')
let url=new URL(request.url,'http://www.xxx.com/search?'); //或者new URL('http://127.0.0.1:9000','search?a=100&b=200')
console.log(url);
// 输出路径
console.log(url.pathname); // /search /favicon.ico
// 二、查询字符串:
// 输出keyword查询字符串
// 如下search属性里是一个字符串形式的查询字符串,有点不方便。所以我们使用searchParams这个对象里面的keyword属性
console.log(url.searchParams.get('keyword')); //  /search 5   /favicon.ico null
});


server.listen(9000,()=>{ 
    console.log('服务已启动...');
});
//注意启动谷歌端口时输入http://127.0.0.1:9000/search?keyword=111; 才能查询到url路径为search字符串为111
相关推荐
糖果店的幽灵19 小时前
软件测试接口测试从入门到精通:HTTP协议详解
软件测试·网络协议·接口测试·http协议·测试基础
数据知道19 小时前
指纹浏览器:DNS 泄漏防范与 WebRTC 本地 IP 屏蔽的底层实现
爬虫·网络协议·tcp/ip·安全·webrtc·数据采集·指纹浏览器
JustHappy1 天前
古法编程秘籍(七):互联网到底是什么?把两台电脑怎么说话搞懂就够了
前端·后端·网络协议
火山上的企鹅1 天前
Codex实战:APP远程升级服务搭建(三)后台管理页面(APK 上传、版本管理、多应用页签)
服务器·网络·数据库·oracle·qgc
caimouse1 天前
Reactos 第 9 章 设备驱动 — 9.5 一组PnP设备驱动模块的实例
网络·windows
袁小皮皮不皮1 天前
3.HCIP OSPF补充知识(优化版)
服务器·网络·数据库·网络协议·智能路由器
志栋智能1 天前
超自动化巡检:知识沉淀与团队协作的新载体
大数据·运维·网络·数据库·人工智能·自动化
酣大智1 天前
策略路由PBR--企业双出口实验
网络·智能路由器·策略路由·pbr
袁小皮皮不皮1 天前
1.HCIP BFD 学习笔记(优化版)
服务器·网络·笔记·网络协议·学习·智能路由器·ip
梁辰兴1 天前
计算机网络基础:数据加密模型
网络·计算机网络·计算机·数据加密·计算机网络基础·梁辰兴·数据加密模型