📜  Node.js urlObject.port API

📅  最后修改于: 2022-05-13 01:56:19.651000             🧑  作者: Mango

Node.js urlObject.port API

Node 中的urlObject.port()方法用于获取主机名中主机组件的数字端口部分。
如果 URL 中不存在端口号,则返回 URL 的端口号,否则返回None

Syntax : urlObject.port()
Return : Returns the URL’s port number or None

示例 #1:在这些示例中,我们展示了 urlObject.port() 方法如何能够从主机名中提取 url 的端口号。

// Importing the module 'url' 
const url = require('url'); 
      
var adr = 
'http://localhost:8080/default.htm?year=2019&month=may'; 
  
// Parse the address: 
var q = url.parse(adr, true); 
  
/* The parse method returns an object containing 
URL properties */
  
console.log(q.port); 

输出 :

8080

示例 #2:

// Importing the module 'url' 
const url = require('url'); 
      
var adr = 
'http://localhost/default.htm?year=2019&month=may'; 
  
// Parse the address: 
var q = url.parse(adr, true); 
  
/* The parse method returns an object containing 
URL properties */
  
console.log(q.port); 

输出 :

null