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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。