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
The correct answer is node's built in URL library.
Specifically
url.format(urlObj)
jQuery does this internally for its AJAX calls. Perhaps there is a way to access the internal functionality.
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).
I built Scheme.js to build url's via javascript objects, please feel free to offer any advancements.