节点 | urlObject.href API
urlObject.href API 用于返回完整的 URL字符串以及协议 (HTTP) 和路径名或其他搜索词。
句法
urlObject.href
For example: 'http://www.geeksforgeeks.com/login/password.html'
Here, Protocol = http
Path = /login
Host = 'www'
File Name = password.html
下面的程序演示了urlObejct.href方法在 Node.js 中的使用:
示例 1:
// Node program to demonstrate the
// urlObject.href API as Setter
// It will return a URL object
const gfg = new URL('https://www.geeksforgeeks.com/login.html ');
// Output the fetched url
console.log(gfg.href);
输出:
https://www.geeksforgeeks.com/login.html
示例 2:
// Node program to demonstrate the
// url.href API as Setter
// Importing the module 'url-parse'
// Use command 'npm install url-parse' in command
// prompt to import this module
var parse = require('url-parse');
var url = parse('https://www.example.com:777/a/b?c=d&e=f#g ');
console.log(url.href);
输出:
https://www.example.com:777/a/b?c=d&e=f#g
注意:上述程序将使用node app.js命令编译运行。
参考: https : //nodejs.org/api/url.html#url_urlobject_href