📜  Node.js 查询字符串

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

Node.js 查询字符串

Query String模块用于提供解析和格式化 URL 查询字符串的实用程序。它可用于将查询字符串转换为 JSON 对象,反之亦然。

查询字符串是 URL 中在问号 (?) 之后开始的部分。

需要模块:您可以使用以下代码包含模块:

const querystring = require('querystring');

注意:它不是全局对象,所以需要显式安装。
安装模块:

npm install querystring

示例 1:使用parse()

Javascript
// Importing the models
import url from 'url'
import querystring from 'querystring'
 
// A URL is taken
let exampleUrl =
'http://www.company.com:81/a/b/c.html?user=GEEKSFORGEEKS&year=2021#p2';
 
//Parse the whole URL
let parsed_Url = url.parse(exampleUrl);
 
// Parse only querystring.
let parsed_queryString = querystring.parse(parsed_Url.query);
 
// Print the result.
console.log("This is parsed URL :",parsed_Url);
 
console.log("This is parsed Query String :",parsed_queryString);


Javascript
// Importing the model
import querystring from 'querystring'
 
// Specify the  object
// to be serialized
const q2=querystring.stringify({
                            name:'Testing',
                            company:'GeeksforGeeks',
                            content:'Article',
                            date:'9thMarch2021'
                           }); 
 
// Print the result.
console.log(q2);


输出:

This is parsed URL : Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.company.com:81',
  port: '81',
  hostname: 'www.company.com',
  hash: '#p2',
  search: '?user=GEEKSFORGEEKS&year=2021',
  query: 'user=GEEKSFORGEEKS&year=2021',
  pathname: '/a/b/c.html',
  path: '/a/b/c.html?user=GEEKSFORGEEKS&year=2021',
  href: 
'http://www.company.com:81/a/b/c.html?user=GEEKSFORGEEKS&year=2021#p2'
}
This is parsed Query String : [Object: null prototype] 
               { user: 'GEEKSFORGEEKS', year: '2021' }

示例 2:使用stringify() 

Javascript

// Importing the model
import querystring from 'querystring'
 
// Specify the  object
// to be serialized
const q2=querystring.stringify({
                            name:'Testing',
                            company:'GeeksforGeeks',
                            content:'Article',
                            date:'9thMarch2021'
                           }); 
 
// Print the result.
console.log(q2);

输出:

name=Testing&company=GeeksforGeeks&
content=Article&date=9thMarch2021

参考: https://nodejs.org/api/querystring.html