【Node.js】querystring 模块

parse

const querystring = require('querystring')
var qs = 'x=3&y=4'
var parsed = querystring.parse(qs)
console.log(parsed)  // [Object: null prototype] { x: '3', y: '4' }

stringify

const querystring = require('querystring')
var qo = {
  x: 3,
  y: 4
}
var parsed = querystring.stringify(qo)
console.log(parsed)  // x=3&y=4

escape/unescape

传入参数在 escape 包装下可以将参数中的有特殊字符进行转义,防止 sql 注入。

const querystring = require('querystring')
var str = 'id=3&city=北京&url=https://www.baidu.com'
var escaped = querystring.escape(str)
console.log(escaped)  // id%3D3%26city%3D%E5%8C%97%E4%BA%AC%26url%3Dhttps%3A%2F%2Fwww.baidu.com
const querystring = require('querystring')
var str = 'id%3D3%26city%3D%E5%8C%97%E4%BA%AC%26url%3Dhttps%3A%2F%2Fwww.baidu.com'
var unescaped = querystring.unescape(str)
console.log(unescaped)  // id=3&city=北京&url=https://www.baidu.com

你可能感兴趣的:(Node.js,node.js)