node.js的url解析和生成

1.url的解析,用url.parse(urlStr)

var url = 'localhost:8080/#/test'
var urlModel = require('url')
console.log(urlModel.parse(url));

打印结果:

Url {
  protocol: 'localhost:',
  slashes: null,
  auth: null,
  host: '8080',
  port: null,
  hostname: '8080',
  hash: '#/test',
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'localhost:8080/#/test'
}

2.url生成,url.format(obj)

var obj = {
    protocol: 'localhost:',
    slashes: null,
    auth: null,
    host: '8080',
    port: null,
    hostname: '8080',
    hash: '#/test',
    search: null,
    query: null,
    pathname: '/',
    path: '/',
    href: 'localhost:8080/#/test'
}

console.log(urlModel.format(obj));

打印结果:

localhost:8080/#/test
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/luguankun/p/12667661.html