A URL is the full address of something on the web. It is made of parts, and each part answers its own question: the scheme (how to talk to the server), the host (which computer), the port (which server on that computer), the path (what you want) and the query string (with which options, after the ?).
TypeScript's built-in URL splits a URL into those parts: you write new URL(address). Every part is a string. Here it is on another URL:
const site = new URL("https://algocademy.com/blog?page=2");
console.log(site.protocol); // https: (the scheme, with its colon)
console.log(site.hostname); // algocademy.com
console.log(site.port); // an empty string: no port is written
console.log(site.search); // ?page=2 (the query, with its ?)
Scheme, host and port tell your program where to send the request. The path and the query travel inside the request and tell the server what you want. When a URL writes no port, the client uses 80 for http and 443 for https.
The starting code builds url for the supplier's cheap products and prints it. Your finished program must print:
URL: http://127.0.0.1:8001/prices?max_price=5 Scheme: http Host: 127.0.0.1 Port: 8001 Path: /prices Query: max_price=5
Your task: split url with new URL and keep the result in parts, then print its scheme, host, port, path and query, each after its label. The scheme is printed without its colon and the query without its ?.
Assignment
Follow the Coding Tutorial to split the supplier's URL into its five parts and print each one.
Hint
Each part is a property of the result: .protocol, .hostname, .port, .pathname and .search. .protocol ends with a colon and .search starts with a ?: remove them with .replace.
Free plan: 30 tutor messages. PRO is unlimited.
