📜  Node.js URL.domainToASCII(1)

📅  最后修改于: 2023-12-03 15:03:14.462000             🧑  作者: Mango

Node.js URL.domainToASCII

The URL.domainToASCII method in Node.js is used to convert a Unicode domain name into an ASCII encoded domain name. This is useful when you need to perform DNS lookups and other operations that require an ASCII domain name.

Syntax
const url = require('url');
url.domainToASCII(hostname)

The hostname parameter is a string that contains the Unicode domain name you want to convert to ASCII.

Return Value

This method returns the ASCII encoded domain name as a string.

Example
const url = require('url');
const hostname = 'سایت.ایران'; // Unicode domain name
const asciiHostname = url.domainToASCII(hostname);
console.log(asciiHostname); // 'xn--mgbh0fb.xn--mgba3a4f16a'

In this example, we convert the Unicode domain name 'سایت.ایران' into its ASCII encoded equivalent using the URL.domainToASCII method. The resulting ASCII encoded domain name is 'xn--mgbh0fb.xn--mgba3a4f16a'.

Notes
  • If the Unicode domain name contains non-ASCII characters that cannot be represented in ASCII form (e.g. a character from a non-Latin script), URL.domainToASCII will convert them using the Punycode encoding system.
  • This method is the inverse of URL.domainToUnicode, which converts an ASCII encoded domain name into a Unicode domain name.