EasyPythonJavaScriptTypeScript

Parse Basic URL

Node.jsURLStrings

Parse '<protocol>://<host>:<port><path>' into an object.
Port and path are optional.

solve('https://example.com:8080/api'){ protocol: 'https', host: 'example.com', port: 8080, path: '/api' }.

If no port: port: null. If no path: path: ''.

Sample tests

Test #1Full URL
Input: ["https://example.com:8080/api"]
Output: {"host":"example.com","path":"/api","port":8080,"protocol":"https"}
Test #2No port no path
Input: ["http://example.com"]
Output: {"host":"example.com","path":"","port":null,"protocol":"http"}
Test #3With path no port
Input: ["ftp://files.io/dir/file"]
Output: {"host":"files.io","path":"/dir/file","port":null,"protocol":"ftp"}
Test #4Just root path
Input: ["https://api.test.com:443/"]
Output: {"host":"api.test.com","path":"/","port":443,"protocol":"https"}
Test #5WebSocket scheme
Input: ["ws://localhost:3000"]
Output: {"host":"localhost","path":"","port":3000,"protocol":"ws"}