首页 > 其他分享 >url-parse

url-parse

时间:2023-11-24 19:55:06浏览次数:43  
标签:string url parse URL using query

url-parse

DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/url-parse package 1.5.10 • Public • Published 2 years ago

url-parse

Version npmBuild StatusCoverage Status

Sauce Test Status

url-parse was created in 2014 when the WHATWG URL API was not available in Node.js and the URL interface was supported only in some browsers. Today this is no longer true. The URL interface is available in all supported Node.js release lines and basically all browsers. Consider using it for better security and accuracy.

The url-parse method exposes two different API interfaces. The url interface that you know from Node.js and the new URL interface that is available in the latest browsers.

In version 0.1 we moved from a DOM based parsing solution, using the <a> element, to a full Regular Expression solution. The main reason for this was to make the URL parser available in different JavaScript environments as you don't always have access to the DOM. An example of such environment is the Worker interface. The RegExp based solution didn't work well as it required a lot of lookups causing major problems in FireFox. In version 1.0.0 we ditched the RegExp based solution in favor of a pure string parsing solution which chops up the URL into smaller pieces. This module still has a really small footprint as it has been designed to be used on the client side.

In addition to URL parsing we also expose the bundled querystringify module.

Installation

This module is designed to be used using either browserify or Node.js it's released in the public npm registry and can be installed using:

npm install url-parse

Usage

All examples assume that this library is bootstrapped using:

'use strict';

var Url = require('url-parse');

To parse an URL simply call the URL method with the URL that needs to be transformed into an object.

var url = new Url('https://github.com/foo/bar');

The new keyword is optional but it will save you an extra function invocation. The constructor takes the following arguments:

  • url (String): A string representing an absolute or relative URL.
  • baseURL (Object | String): An object or string representing the base URL to use in case url is a relative URL. This argument is optional and defaults to location in the browser.
  • parser (Boolean | Function): This argument is optional and specifies how to parse the query string. By default it is false so the query string is not parsed. If you pass true the query string is parsed using the embedded querystringify module. If you pass a function the query string will be parsed using this function.

As said above we also support the Node.js interface so you can also use the library in this way:

'use strict';

var parse = require('url-parse')
  , url = parse('https://github.com/foo/bar', true);

The returned url instance contains the following properties:

  • protocol: The protocol scheme of the URL (e.g. http:).
  • slashes: A boolean which indicates whether the protocol is followed by two forward slashes (//).
  • auth: Authentication information portion (e.g. username:password).
  • username: Username of basic authentication.
  • password: Password of basic authentication.
  • host: Host name with port number. The hostname might be invalid.
  • hostname: Host name without port number. This might be an invalid hostname.
  • port: Optional port number.
  • pathname: URL path.
  • query: Parsed object containing query string, unless parsing is set to false.
  • hash: The "fragment" portion of the URL including the pound-sign (#).
  • href: The full URL.
  • origin: The origin of the URL.

Note that when url-parse is used in a browser environment, it will default to using the browser's current window location as the base URL when parsing all inputs. To parse an input independently of the browser's current URL (e.g. for functionality parity with the library in a Node environment), pass an empty location object as the second parameter:

var parse = require('url-parse');
parse('hostname', {});

Url.set(key, value)

A simple helper function to change parts of the URL and propagating it through all properties. When you set a new host you want the same value to be applied to port if has a different port number, hostname so it has a correct name again and href so you have a complete URL.

var parsed = parse('http://google.com/parse-things');

parsed.set('hostname', 'yahoo.com');
console.log(parsed.href); // http://yahoo.com/parse-things

It's aware of default ports so you cannot set a port 80 on an URL which has http as protocol.

Url.toString()

The returned url object comes with a custom toString method which will generate a full URL again when called. The method accepts an extra function which will stringify the query string for you. If you don't supply a function we will use our default method.

var location = url.toString(); // http://example.com/whatever/?qs=32

You would rarely need to use this method as the full URL is also available as href property. If you are using the URL.set method to make changes, this will automatically update.

Testing

The testing of this module is done in 3 different ways:

  1. We have unit tests that run under Node.js. You can run these tests with the npm test command.
  2. Code coverage can be run manually using npm run coverage.
  3. For browser testing we use Sauce Labs and zuul. You can run browser tests using the npm run test-browser command.

License

MIT

Readme

Keywords

标签:string,url,parse,URL,using,query
From: https://www.cnblogs.com/sexintercourse/p/17854632.html

相关文章

  • Adaptive Sparse Pairwise Loss for Object Re-Identification
    https://blog.csdn.net/amusi1994/article/details/130037400tripletloss中需要计算每个样本之间的距离,从而计算出loss,作者认为同一类的某些样本可能存在有害的信息,所以不应该将所有样本都用于计算loss。作者提出的SPloss中只计算挑选出来的样本的距离,从而得到loss。我们提出......
  • ez_curl【代码审计】
    ez_curl【代码审计】[难度:4]题目描述代码审计类题目,附上代码:<?phphighlight_file(__FILE__);$url='http://back-end:3000/flag?';**$input=file_get_contents('php://input');****$headers=(array)json_decode($input)->headers;**for($i=0;$i<......
  • 解决POST表单提交报错 Content type 'application/x-www-form-urlencoded;charset=UTF
    百度发现application/x-www-form-urlencoded;charset=UTF-8是以键值对拼接的形式,即前端传过来的是键值对形式前端代码:底层使用的vue中的axios发送的请求importrequestfrom'@/utils/request'exportdefault{getTeacherList(page,limit,teacherQuery){returnreque......
  • 稀疏数组(sparseArray)
    稀疏数组1.二维数组转成稀疏数组//将二维数组转成稀疏数组//1.得到非零个数sumintsum=0;for(inti=0;i<chessArray.length;i++){for(intj=0;j<chessArray.length;j++){if(chessArray[i]......
  • C++ LibCurl实现Web指纹识别
    Web指纹识别是一种通过分析Web应用程序的特征和元数据,以确定应用程序所使用的技术栈和配置的技术。这项技术旨在识别Web服务器、Web应用框架、后端数据库、JavaScript库等组件的版本和配置信息。通过分析HTTP响应头、HTML源代码、JavaScript代码、CSS文件等,可以获取关于Web应用程......
  • C++ LibCurl实现Web隐藏目录扫描
    LibCurl是一个开源的免费的多协议数据传输开源库,该框架具备跨平台性,开源免费,并提供了包括HTTP、FTP、SMTP、POP3等协议的功能,使用libcurl可以方便地进行网络数据传输操作,如发送HTTP请求、下载文件、发送电子邮件等。它被广泛应用于各种网络应用开发中,特别是涉及到数据传输的场景。......
  • Linux:ping、curl、telnet作用异同
    学习自:Linu网络判断指令ping、curl、telnet的区别与应用场景_ping和curl的区别_饮风丶欤的博客-CSDN博客telnet和curl和ping的区别_curl和telnet的区别-CSDN博客1、curl在Linux中curl是一个利用URL规则在命令行中工作的文件传输工具,是一个http命令行工具,可以帮助我们在服务......
  • Nginx实现基于请求URL的请求重写配置
    在Nginx中,可以使用rewrite指令来进行请求重写。其基本语法如下:复制1rewriteregexreplacement[flag];其中,regex表示正则表达式,用于匹配当前请求URL;replacement表示目标URL,替换原来的URL;flag是可选的标志位,用于控制重写的行为。例如,下面的重写规则可以将以“/pa......
  • yolo v5 下载新数据集被防火墙proxy挡住,如何设置proxy. torch.hub.download_url_to_fi
    当我们想运行yolov5时候,我们发现有的时候,由于网关问题,proxy会成为阻碍。例如如下错误;  将代码如下修改,就能改好:1.原始代码: 2.增加proxy设置: importurllib.requestimporttorch.hub#设置代理信息proxy_support=urllib.request.ProxyHandler({'http':'http......
  • chrome:在url中指定搜索引擎
    1、浏览器设置里面找到搜索引擎,添加网站搜索,点击添加默认搜索就会添加到搜索引擎中2、然后在url中输入快捷词+空格,然后在输入要搜索的内容即可 ......