url模块用于解析url
例:
const url = require('url'); var api = 'https://www.baidu.com?name=zhangsan&age=21'; // console.log(url.parse(api)); /* Url { protocol: 'https:', slashes: true, auth: null, host: 'www.baidu.com', port: null, hostname: 'www.baidu.com', hash: null, search: '?name=zhangsan&age=21', query: 'name=zhangsan&age=21', pathname: '/', path: '/?name=zhangsan&age=21', href: 'https://www.baidu.com/?name=zhangsan&age=21' } */ //我们再传递一个参数true,即可把query封装成一个对象 // console.log(url.parse(api,true)); /* Url { protocol: 'https:', slashes: true, auth: null, host: 'www.baidu.com', port: null, hostname: 'www.baidu.com', hash: null, search: '?name=zhangsan&age=21', query: [Object: null prototype] { name: 'zhangsan', age: '21' }, pathname: '/', path: '/?name=zhangsan&age=21', href: 'https://www.baidu.com/?name=zhangsan&age=21' } */ var temp = url.parse(api,true).query; console.log(temp);//[Object: null prototype] { name: 'zhangsan', age: '21' } console.log(temp.name);//zhangsan console.log(temp.age);//21
标签:21,url,age,模块,null,zhangsan,name From: https://www.cnblogs.com/qianduanLamp/p/16742061.html