📜  Node.js urlObject.query API

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

Node.js 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