引入模块
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'
}