node_url模块_querystring模块
作者:互联网
//引入模块 let http = require('http') let fs = require('fs') //引入url模块 let url = require('url') // 引入querystring模块 let querystring = require('querystring') let server = http.createServer((req, res)=>{ //将url地址解析成url对象 //pathname:返回url地址 //query:返回参数字符串(从?后面开始截取字符串) let urlObj = url.parse(req.url) console.log('urlObj',urlObj) //将query参数解析成obj对象 //count=1&name=jack&age=18 ==> {count: '1',name: 'jack', age:'18'} let querystringObj = querystring.parse(urlObj.query) // 等同于letlet querystringObj = querystring.decode(urlObj.query) console.log('querystringObj',querystringObj) // 反解析,将对象解析成字符串 let parmsString = querystring.stringify(querystringObj) // 等同于let parmsString = querystring.encode(querystringObj) console.log('parmsString',parmsString) //数据接口监听 if (urlObj.pathname == '/interface') {res.end("{name: 'jack', age: 18, job: 'web'}")} else { //静态资源管理 fs.readFile(__dirname + '/src' + urlObj.pathname, (err, data)=> { if (err) return err else res.end(data) }) } }).listen(8888)
标签:node,querystringObj,url,querystring,let,模块,query,urlObj 来源: https://www.cnblogs.com/JunLan/p/12411738.html