The url.port is an inbuilt application programming interface of class URL within url module which is used to get and set the port portion of the URL.the port value may be a number or a string containing a number in the range 0 to 65535 (inclusive). Setting the value to the default port of the URL objects given protocol will result in the port value becoming the empty string (”).
The port value can be an empty string in which case the port depends on the protocol/scheme:
Protocol port “ftp” 21 “file” “gopher” 70 “http” 80 “https” 443 “ws” 80 “wss” 443
Upon assigning a value to the port, the value will first be converted to a string using .toString().
If that string is invalid but it begins with a number, the leading number is assigned to the port. If the number lies outside the range denoted above, it is ignored.
Return value: It gets and sets the port portion of the URL.
Below programs illustrate the use of url.port Method:
Example 2: If the port number is half numeric and half alphabetic
NOTE: The above program will compile and run by using the myapp.js command on Node.