Nodejs的学习笔记(2)

一、node内置模块url

url:统一资源定位符 (详细内容请看官网https://nodejs.org/dist/latest-v12.x/docs/api/url.html)

1、url.parse和url.format() (类比json 记忆)
  • url.parse 将url字符串转化成对象

    const url = require('url');
    let urlString = 'https:47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567#nihao'
    let urlObj = url.parse(urlString)
    console.log(urlObj);
    //打印结果
    Url {
          
          
      protocol: 'https:',
      slashes: null,
      auth: null,
      host: null,
      port: null,
      hostname: null,
      hash: '#nihao',
      search: '?us=123&ps=567',
      query: 'us=123&ps=567',
      pathname: '47.95.207.1:3000/fcj/recommend/hot/hehe',
      path: '47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567',
      href: 'https:47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567#nihao'
    }
    
  • url.format将url对象转字符串

    const url = require('url');
    var obj = {
          
          
      protocol: 'https:',
      slashes: null,
      auth: null,
      host: null,
      port: null,
      hostname: null,
      hash: '#nihao',
      search: '?us=123&ps=567',
      query: 'us=123&ps=567',
      pathname: '47.95.207.1:3000/fcj/recommend/hot/hehe',
      path: '47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567',
      href: 'https:47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567#nihao'
    }
    let str =url.format(obj);
    console.log(str);
    //打印结果
    'https:47.95.207.1:3000/fcj/recommend/hot/hehe?us=123&ps=567#nihao'
    

知识点补充 json: json是一种数据格式,json对象是保证json的格式的对象,json字符串是json格式的字符串。

猜你喜欢

转载自blog.csdn.net/literarygirl/article/details/107285737