nodejs 之 url 模块使用

引入模块

	const URL= require('url');

URL.fromat(urlObj)

将URL对象转换成字符串链接

	const http = require('http');
	const URL = require('url');
	const server = http.createServer();
	const urlObj = {
 	 protocol: 'https',
 	 hostname: 'example.com',
	  pathname: '/some/path',
	  query: {
 	   page: 1,
 	   format: 'json'
 	 }
	};
	server.on('request', function (request, response) {
	  console.log(URL.format(urlObj));
	})
	server.listen(3000, function () {
	  console.log('Server running at http://127.0.0.1:3000/');
	})
  • 运行结果
	https://example.com/some/path?page=1&format=json

URL.resolve(from, to)

为URL或 href 插入 或 替换原有的标签

  • require('url').resolve(from, to)
	require('url').resolve('/one/two/three', 'four')
	/* '/one/two/four' */
	require('url').resolve('http://example.com/', '/one')
	/* 'http://example.com/one' */
	require('url').resolve('http://example.com/one', '/two')
	/* 'http://example.com/two' */

URL.parse(urlString, parseQueryString, slashesDenoteHost)

将字符串地址转换成URL对象

  • require('url').parse(urlString[, parseQueryString[, slashesDenoteHost]])

    • urlString为要解析的URL字符串

    • parseQueryString(默认为false)

      • true => 返回的URL对象中 query 为一个对象
      • false => 返回的URL对象中 query 为一个未解析未解码字符串
    • slashesDenoteHost(默认为false)

      特别注意:当遇到以 // 开头的URL时,才有区别
      设置为true时,表示 // 与 第一个/ 之间的字符进行解析,否则直接输出为pathname

    • true =>
      	const url = '//foo/bar'
      	// => {host: 'foo', pathname: '/bar'} 
      
    • false =>
      	const url = '//foo/bar'
      	// => {pathname: '//foo/bar'}
      

http.js

	const http = require('http');
	const URL = require('url');
	const server = http.createServer();
	server.on('request', function (request, response) {
  		console.log(URL.parse(request.url, true));
  		console.log('==============');
  		console.log(URL.parse(request.url, false));
	})
	server.listen(3000, function () {
  		console.log('Server running at http://127.0.0.1:3000/');
	})

jsonp.html

	<!DOCTYPE html>
	<html lang="en">

	<head>
	  <meta charset="UTF-8">
	  <meta name="viewport" content="width=device-width, initial-scale=1.0">
	  <title>JSONP</title>
	</head>

	<body>
 	 <h1>JSONP方案</h1>
 	 <script>
 	   var script = document.createElement('script');
 	   script.type = 'text/javascript';
 	   script.src = 'http://127.0.0.1:3000/jsonp.html?callback=handleCallback';
 	   document.head.appendChild(script);
 	   function handleCallback(res) {
  	    alert(JSON.stringify(res))
 	   }
 	 </script>
	</body>
	</html>

运行结果

Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: '127.0.0.1:3000',
  port: '3000',
  hostname: '127.0.0.1',
  hash: null,
  search: '?callback=handleCallback',
  
  ========================= 两者的区别 ===========================
  query: 'callback=handleCallback',
  ========================= 两者的区别 ===========================
  
  pathname: '/pages/jsonp.html',
  path: '/pages/jsonp.html?callback=handleCallback',
  href:
   'http://127.0.0.1:3000/pages/jsonp.html?callback=handleCallback' 
}
==========================
Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: '127.0.0.1:3000',
  port: '3000',
  hostname: '127.0.0.1',
  hash: null,
  search: '?callback=handleCallback',
  
  ========================= 两者的区别 ===========================
  query: [Object: null prototype] { callback: 'handleCallback' },
  ========================= 两者的区别 ===========================    
  
  pathname: '/pages/jsonp.html',
  path: '/pages/jsonp.html?callback=handleCallback',
  href:
   'http://127.0.0.1:3000/pages/jsonp.html?callback=handleCallback' 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值