http模块 获取http请求报文中的路径 与 查询字符串

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

方法一:

一、获取路径

例如:我在启动谷歌端口时输入http://127.0.0.1:9000 后接了 "/search?keyword=111" 我想要查询到url路径为search字符串为111。方法如下:

1.导入url模块

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

2.创建服务对象

3.运用url.parse解析查询字符串为一个对象

javascript 复制代码
const server=http.createServer((request,response)=>{
    response.end('url'); //end设置响应体
    // console.log(request.url); 
//虽然request.url已包含属性和查询字符串 /search?keyword=5&num=1 /favicon.ico  但使用不便,若只需其中一个不好提取,于是用到了如下路径和字符串的单独查询方法:
    // 2.使用url.parse()方法解析request.url里面的内容
//url.parse() 方法接受两个参数:
     //参数一:要解析的URL字符串。
     //参数二:布尔值true/false ,表示是否要解析查询字符串为一个对象。如果为 true,则URL的查询部分(通常是?后面的部分)将被解析为一个键值对象。
    let res=url.parse(request.url,true); 
    // console.log(res); //输出可以看到 路径为pathname: '/search', pathname: '/favicon.ico',  后接true,使查询字符串变成了一个对象
let pathname=res.pathname;
    console.log(pathname); //  /search  /favicon.ico   可以看到pathname里就是我们所要的路径
});
//启动服务
server.listen(9000,()=>{ 
    console.log('服务已启动...');
});

二、获取查询字符串

javascript 复制代码
// 1.导入url模块
const url=require('url');
// 创建服务对象:
const server=http.createServer((request,response)=>{
    response.end('url'); //end设置响应体
    let res=url.parse(request.url,true); 
    let keyword=res.query.keyword;
//我们输出上文的 console.log(res);可以看到 查询字符串是 query: [Object: null prototype] { keyword: '5', num: '1' }的形式的,所以是.query.keyword
    console.log(keyword); //5 undefined  第一个url的keyword为5,第二个url里面没有包含keyword这个参数所以为undefined
});
server.listen(9000,()=>{ 
    console.log('服务已启动...');
});

方法二:

一、获取路径

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'); 
//或者newURL('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
});


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

二、获取查询字符串

javascript 复制代码
// 获取http请求报文中的请求路径与查询字符串2:***
const http=require('http');

const server=http.createServer((request,response)=>{
    response.end('url new');
// (1)实例化一个URL对象 必须由域名或ip、路径与字符串组成
let url=new URL(request.url,'http://www.xxx.com/search?'); 
// 二、查询字符串:
// 输出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
相关推荐
chilavert318几秒前
技术演进中的开发沉思-28 MFC系列:关于C++
开发语言·c++·mfc
witton2 分钟前
C语言使用Protobuf进行网络通信
c语言·开发语言·游戏·c·模块化·protobuf·protobuf-c
归于尽11 分钟前
回调函数在Node.js中是怎么执行的?
前端·javascript·node.js
搬砖天才、14 分钟前
SpringGateway网关增加https证书验证
网络协议·http·https
cui_win14 分钟前
【网络】Linux 内核优化实战 - net.ipv4.tcp_dsack
linux·网络·tcp/ip
2501_9153743520 分钟前
WHIP(WebRTC HTTP Ingestion Protocol)详解
网络协议·tcp/ip·udp
黄焖鸡能干四碗20 分钟前
系统安全设计方案,软件系统安全设计方案
开发语言·数据库·安全·vue·系统安全
dragoooon3440 分钟前
C++——string的了解和使用
c语言·开发语言·c++·学习·学习方法
格林威1 小时前
Baumer工业相机堡盟工业相机如何通过DeepOCR模型识别判断数值和字符串的范围和相似度(C#)
开发语言·人工智能·python·数码相机·计算机视觉·c#·视觉检测
sanggou1 小时前
InterSystems IRIS安装部署
开发语言