📜  节点 | urlObject.query API

📅  最后修改于: 2022-05-13 01:56:57.034000             🧑  作者: Mango

节点 | urlObject.query API

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