当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js URL.origin用法及代码示例


url.origin是url模块中URL类的内置应用程序编程接口(API)。 url.origin API用于获取URL来源的只读序列化。

Syntax:url.origin
url:It is an object created by URL constructor.

范例1:

//Importing the url module 
const url = require('url'); 
  
//Creating an URL_1 object with URL constructor. 
const URL_1 = new URL("https://www.geeksforgeeks.org/geeks"); 
  
//Getting origin of above created URL_1 object 
console.log(URL_1.origin);

输出:


https://www.geeksforgeeks.org

注意:我们无法使用url.origin API设置URL的来源。如果我们尝试这样做,那么它将被忽略并且不会影响到原点。

范例2:

//Importing the url module 
const url = require('url'); 
  
//Creating an URL_1 object with URL constructor. 
const URL_1 = new URL("https://www.geeksforgeeks.org/geeks"); 
  
//Getting origin of above created URL_1 object 
console.log(URL_1.origin); 
  
//Setting URL_1 origin to https://www.geeks.com 
URL_1.origin = "https://www.geeks.com"; 
  
//Getting origin after setting URL_1 origin 
console.log(URL_1.origin);

输出:

https://www.geeksforgeeks.org


相关用法


注:本文由纯净天空筛选整理自AshishkrGoyal大神的英文原创作品 Node | URL.origin API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。