最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - typescript get hostname, protocol, port from a string containging url - Stack Overflow

matteradmin3PV0评论

if I have a string like http://localhost:8080 and I want to extract the different parts of the of url, like the protocol http, domain name (localhost) and port (8080), what is the best way to do in typescript?

I can do a simple parse of the string to get the different values. But not sure if that is the best/cleanest way to do it. Should I first convert this string to a valid URL (using some library) and then get the hostname, protocol and port from it? Java would have such libraries that would easily let me do that but how do I achieve the same with typescript?

if I have a string like http://localhost:8080 and I want to extract the different parts of the of url, like the protocol http, domain name (localhost) and port (8080), what is the best way to do in typescript?

I can do a simple parse of the string to get the different values. But not sure if that is the best/cleanest way to do it. Should I first convert this string to a valid URL (using some library) and then get the hostname, protocol and port from it? Java would have such libraries that would easily let me do that but how do I achieve the same with typescript?

Share Improve this question edited May 4, 2017 at 19:32 Nitzan Tomer 165k51 gold badges329 silver badges306 bronze badges asked May 4, 2017 at 19:13 user1892775user1892775 2,1218 gold badges39 silver badges61 bronze badges 3
  • What's your environment? (Example) – David Sherret Commented May 4, 2017 at 19:16
  • angular 2 is my front end and backend is node – user1892775 Commented May 4, 2017 at 19:17
  • 1 First of all, asking for typescript won't yeld much results for you. Ask for JavaScript; Second of all, what technology you are asking about? Browser, server, IoT? – andrew.fox Commented May 4, 2017 at 19:18
Add a ment  | 

3 Answers 3

Reset to default 6

You can use the URL class:

let url = new URL("http://localhost:8080");
console.log(url.protocol); // http:
console.log(url.host); // localhost:8080
console.log(url.port); // 8080

(code in playground)

It is not implemented in all browsers but you can find polyfills for it if you want to work with it.


Edit

In node you can do this:

import { URL } from "url";

let url = new URL("http://localhost:8080");
console.log(url.protocol); // http:
console.log(url.host); // localhost:8080
console.log(url.port); // 8080

If you get this pilaiton error:

error TS2307: Cannot find module 'url'

Then you need the node definitions:

npm install @types/node

You can use HTMLAnchorElement in this purposes

let a = document.createElement("a");
a.href = "http://localhost:8080";

console.log(a.protocol);
console.log(a.host);
console.log(a.port);

If you are using Node.js 6 or lower,

import * as url from 'url';
const { protocol, host, port } = url.parse('http://localhost:8080');
console.log(protocol); // http:
console.log(host); // localhost:8080
console.log(port); // 8080

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far