Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js Punycode
What is Punycode
Punycode is an encoding syntax which is used to convert Unicode (UTF-8) string of characters to basic ASCII string of characters. Since host names only understand ASCII characters so Punycode is used. It is used as an internationalized domain name (IDN or IDNA). Let's understand it with an example:
Assume if you search for mañana.com in your browser so your browser (which is IDNA enabled) first convert this to punycode xn--maana-pta.com because the character ñ is not allowed in regular domain name. It is not supported in older versions.
Punycode in Node.js
Punycode.js is bundled with Node.js v0.6.2 and later versions. If you want to use it with other Node.js versions then use npm to install punycode module first. You have to use require ('punycode') to access it.
Syntax:
- punycode = require('punycode');
punycode.decode(string)
It is used to convert a Punycode string of ASCII symbols to a string of Unicode symbols.
File: punycode_example1.js
- punycode = require('punycode');
- console.log(punycode.decode('maana-pta'));
Output:
punycode.encode(string)
It is used to convert a string of Unicode symbols to a Punycode string of ASCII symbols.
File: punycode_example2.js
- punycode = require('punycode');
- console.log(punycode.encode('☃-⌘'));
Output:
punycode.toASCII(domain)
It is used to convert a Unicode string representing a domain name to Punycode. Only the non-ASCII part of the domain name is converted.
File: punycode_example3.js
- punycode = require('punycode');
- console.log(punycode.toASCII('mañana.com'));
Output:
punycode.toUnicode(domain)
It is used to convert a Punycode string representing a domain name to Unicode. Only the Punycoded part of the domain name is converted.
File: punycode_example4.js
- punycode = require('punycode');
- console.log(punycode.toUnicode('xn--maana-pta.com'));
Output: