urlObject.query是不带ASCII问号(?)的返回的查询字符串,或由名为parse()方法的querystring模块返回的对象。 Url.parse()方法用于检查查询是字符串还是对象。本质上,将传递给url.parse()方法的参数(parseQueryString)告知查询的性质。
用法
urlObject.query
注意:如果此方法作为字符串返回,则不执行解码查询字符串;如果返回对象,则对两个键,值对进行解码。
例:
'query=string' or {'query':'object'} 'http://localhost:8000/gfg.html?name:GFG' In the above URL, the name is the query and GFG is the string.
以下示例程序旨在说明Node.js中url.query方法的使用:
范例1:
// Node program to demonstrate the
// url.query API as Setter
// Importing the module 'url'
var url = require('url');
// Set the URL from which the queryString will be fetched
var address = 'http://localhost:8000/gfg.html?month=Decemeber';
// Parse the address
var q = url.parse(address, true);
// The query property returns an object with all the
// querystring parameters as properties
var query = q.query;
var month = query.month;
console.log(month);
输出:
December
范例2:
// Node program to demonstrate the
// url.query API as Setter
// Importing the module 'url'
var url = require('url');
// Set the URL from which the queryString will be fetched
var address = 'http://localhost:8000/gfg.html?month=Decemeber&year=2019';
// Parse the address
var q = url.parse(address, true);
// The query property returns an object with all the
// querystring parameters as properties
var query = q.query;
var year = query.year;
console.log(year);
输出:
2019
参考: https://nodejs.org/api/url.html#url_urlobject_query
相关用法
- Node.js URL.pathToFileURL用法及代码示例
- Node.js URL.fileURLToPath用法及代码示例
- Node.js URLSearchParams.has()用法及代码示例
注:本文由纯净天空筛选整理自Abhishek7大神的英文原创作品 Node | urlObject.query API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。