可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've created a function that can build nested urls like so. I was wondering if there existed a more mainstream library to build urls / uris like this. I'd rather use a standard.
utility.urlConstruct({ "scheme": "https://", "domain": "domain.com", "path": "/login", "query":{ "user":"thomasreggi", "from":utility.urlConstruct({ "scheme": "https://", "domain": "redirect.com", "path": "/funstuff", }), } });
Spits out
https://domain.com/login?user=thomasreggi&from=https%3A%2F%2Fredirect.com%2Ffunstuff
回答1:
The correct answer is node's built in URL library.
Specifically
url.format(urlObj)
回答2:
jQuery does this internally for its AJAX calls. Perhaps there is a way to access the internal functionality.
回答3:
Al least there are standard conventions:
- the scheme (or protocol) excludes //
- your domain is called host (and can include sub-domains or port)
- path is called pathname
- query is a list of parameters
- you might also need a hash
Then it is pretty straightforward, so no real need for a library. You'll find some libraries out there, like jsuri, but their purpose is more to help for parsing than construct (for example to address cross-browser inconsistencies with pathname).
回答4:

I built Scheme.js to build url's via javascript objects, please feel free to offer any advancements.