Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
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 Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
 


  

 
Node.js Punycode
Question Posted on 13 Apr 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Punycode


Node.js Punycode

Node.js Punycode


When we say about the Punycode, it is an encoding syntax which is mainly used to convert Unicode (UTF-8) string of characters to basic ASCII string of characters. As we knows that host names only understand ASCII characters so we used Punycode. And it is used as an internationalized domain name(IDN or IDNA). To understand this we will take an example:-

Let assume if we want to search an bañana.com in our browser so our browser(which is IDNA enabled) first we need to convert this to punycode xn--baana-pta.com because the character ñ is not allowed in regular domain name. And this is not supported in older versions.


Punycode in Node.js


When we say Punycode.js it is bundled with Node.js v0.6.2 and its later verions. And you want to use it with other Node.js versions then you can use npm to install punycode module first. You have to use require ('punycode') to access it.


punycode = require('punycode');



punycode.decode(string)


This 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('baana-pta'));


Output:


punycode encode

punycode.encode(string)


This 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 decode

punycode.toASCII(domain)


This 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('bañana.com'));


Output:


punycode ASCII decode

punycode.toUnicode(domain)


This 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--baana-pta.com'));


Output:


punycode ASCII code





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |