remove port number from url node js

To remove the port number from a URL in Node.js, you can follow these steps:

  1. First, you need to parse the URL using the built-in url module. You can do this by requiring the module and using the parse method. Here's an example:
const url = require('url');
const parsedUrl = url.parse('http://example.com:8080/path');
  1. Next, you can access the hostname property of the parsed URL. This property will contain the domain without the port number. Here's an example:
console.log(parsedUrl.hostname); // Output: example.com
  1. If you want to reconstruct the URL without the port number, you can use the format method of the url module. This method allows you to pass in the modified properties of the parsed URL. Here's an example:
const modifiedUrl = url.format({
  protocol: parsedUrl.protocol,
  hostname: parsedUrl.hostname,
  pathname: parsedUrl.pathname,
  search: parsedUrl.search,
  hash: parsedUrl.hash
});

console.log(modifiedUrl); // Output: http://example.com/path

By following these steps, you can successfully remove the port number from a URL in Node.js.