当前位置:网站首页>NodeJS 解析 GET 请求 url 字符串
NodeJS 解析 GET 请求 url 字符串
2022-06-13 04:23:00 【Lete乐特】
前言
原文: https://blog.imlete.cn/article/Parse-get-url-string.html
封装开源地址: Body-Data
当你使用querystring.parse()或者是url.parse()时会得到一个警告@deprecated — since v11.0.0 - Use the WHATWG URL API.
由于 url.parse() 方法使用一种宽松的非标准算法来解析网址字符串,因此可能会引入安全问题
具体来说,已经确定了主机名欺骗以及用户名和密码处理不当的问题
url.parse()在v11.0.0开始被打上@deprecated注解(也不知道叫做注解合不合理,可能是 java 写多了吧),在v16.0.0后被完全移除,同时还一并移除的有url.format()url.resolve()
/** * 在 querystring 的 TS 源码开头已经被打上了 @deprecated 具体说明时候被完全移除目前还不得而知 * 部分注释已被我被删除 * @deprecated Legacy * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/querystring.js) */
declare module "querystring" {
}
declare module "url" {
/** * 在 url 的 TS 源码里 parse() 已经被打上了 @deprecated 从v11.0.0开始,到v16.0.0被移除 * 部分注释已被我被删除 * @since v0.1.25 * @deprecated Legacy: Use the WHATWG URL API instead. * @param urlString The URL string to parse. * 部分注释已被我被删除 */
function parse(urlString: string): UrlWithStringQuery;
}
为什么?
这是因为 nodejs 的url.parse方法采用的传统的urlObject,不符合URL 标准,因此被弃用
WHATWG 是 (Web Hypertext Application Technology Working GroupWeb 超文本应用程序技术工作组)
使用 URL
使用 URL 类对 url 进行操作,详细文档见(可将域名后的 org 改为 cn 看中文文档)
const urlStr = "https://example.org/foo?name=zhangsan&age=18";
const url = new URL(urlStr);
// searchParams 用的是 URLSearchParams API
const name = url.searchParams.get("name");
const age = url.searchParams.get("age");
console.log(name); // zhangsan
console.log(age); // 18
正文
前面脆皮了那么多内容,现在才是本文的主要内容
我们可以这么写来解析 get 请求的参数
访问: http://localhost:3000/foo?name=zhangsan&age=18
因为我比较懒,我就不写那么多示例代码了,全部塞到一个代码示例里,大家勉强看一下注释吧
为了方便,我封装成了一个库,可以获取POST、GET请求的参数,觉得可以的话可以点个star支持一下
项目地址: Body-Data: https://github.com/Lete114/Body-Data
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/favicon.ico") return res.end();
// req.url含问号前面的内容: /foo?name=zhangsan&age=18
console.log(req.url);
// 也可以使用以下两行代码移除 ?"问号" 前面的内容
// const index = url.indexOf('?') + 1
// const string = url.substring(index)
// const query = new URLSearchParams(string);
// 这是为了解析出search,search不含 ?"问号" 前面的内容
// 也就是 name=zhangsan&age=18
const url = new URL(req.url, `http://${
req.headers.host}/`);
const query = new URLSearchParams(url.search);
console.log(query.get("name")); // zhangsan
console.log(query.entries()); // URLSearchParams Iterator { [ 'name', 'zhangsan' ], [ 'age', '18' ] }
console.log(query.keys()); // URLSearchParams Iterator { 'name', 'age' }
// 定义一个空对象
const params1 = {
};
// 如果for循环内只有1行代码是可以简写成1行的,如下
for (const i of query.keys()) params1[i] = query.get(i);
// 使用entries()也可以
const params2 = {
};
for (const i of query.entries()) params2[i[0]] = i[1];
// 使用 Object.fromEntries()
const params3 = Object.fromEntries(query);
console.log(params1); // { name: 'zhangsan', age: '18' }
console.log(params2); // { name: 'zhangsan', age: '18' }
console.log(params3); // { name: 'zhangsan', age: '18' }
res.end(JSON.stringify(params3));
});
server.listen(3000);
边栏推荐
- Analysis of the implementation principle of an open source markdown to rich text editor
- Explanation of line segment tree
- February 25, 2021 (Archaeology 12 year Landbridge provincial competition)
- SQL advanced challenge (1 - 5)
- [notes] summarize common horizontal and vertical centering methods
- Click change color to change subscript
- Notes on software test for programmers -- basic knowledge of software development, operation and maintenance
- Application of dagger2 learning module (II)
- Time format method on the official demo of uniapp
- Dumi builds a document blog
猜你喜欢

Unity shader learning 004 shader debugging platform difference third-party debugging tools

Introduction to applet Basics (dark horse learning notes)

出现Could not find com.scwang.smart:refresh-layout-kernel:2.0.3.Required by: project :app 无法加载第三方包情况

Dumi builds a document blog

Record a troubleshooting process - video call cannot be picked up

Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich

一款开源的Markdown转富文本编辑器的实现原理剖析

【LeetCode】860. Change with lemonade (2 brushes for wrong questions)

Principle, composition and functions of sensors of Dajiang UAV flight control system

重读经典:《End-to-End Object Detection with Transformers》
随机推荐
H5 the blue background color appears when clicking the picture
【自动化测试】关于unittest你需要知道的事
Idea Download
Dagger2学习之Module的应用(二)
Redis
Sword finger offer 56 - I. number of occurrences in the array
knife4j aggregation 2.0.9支持路由文档自动刷新
Collection of wrong questions in soft test -- morning questions in the first half of 2010
Interpretation and implementation of proxy mode
Set properties \ classes
[sword finger offer] interview question 25 Merge two ordered linked lists
SS selector
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-3 personal solutions
【Flutter 问题系列第 67 篇】在 Flutter 中使用 Get 插件在 Dialog 弹窗中不能二次跳转路由问题的解决方案
是“凯撒密码”呀。(*‘▽‘*)*
Notes on uni app
第007天:go语言字符串
Click change color to change subscript
Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-2 personal solutions