Node.js url.parse result back to string
Solution 1
If you look at the latest documentation, you can see that url.format
behaves in the following way:
search
will be used in place ofquery
query
(object; see querystring) will only be used ifsearch
is absent.
And when you modify query
, search
remains unchanged and it uses it. So to force it to use query
, simply remove search
from the object:
var url = require("url");
var parts = url.parse("http://test.com?page=25&foo=bar", true);
parts.query.page++;
delete parts.search;
console.log(url.format(parts)); //http://test.com/?page=26&foo=bar
Make sure you're always reading the latest version of the documentation, this will save you a lot of trouble.
Solution 2
Seems to me like it's a bug in node. You might try
// in requires
var url = require('url');
var qs = require('querystring');
// later
var parts = url.parse(req.url, true);
parts.query['page'] = 25;
parts.query = qs.stringify(parts.query);
console.log("Link: ", url.format(parts));
Solution 3
The other answer is good, but you could also do something like this. The querystring
module is used to work with query strings.
var querystring = require('querystring');
var qs = querystring.parse(parts.query);
qs.page = 25;
parts.search = '?' + querystring.stringify(qs);
var newUrl = url.format(parts);
Related videos on Youtube
benui
Unreal Engine UI programmer working at Brace Yourself Games. I share UI-related UE4 tutorials on my website.
Updated on July 09, 2022Comments
-
benui almost 2 years
I am trying to do some simple pagination. To that end, I'm trying to parse the current URL, then produce links to the same query, but with incremented and decremented
page
parameters.I've tried doing the following, but it produces the same link, without the new
page
parameter.var parts = url.parse(req.url, true); parts.query['page'] = 25; console.log("Link: ", url.format(parts));
The documentation for the URL module seems to suggest that
format
is what I need but I'm doing something wrong.I know I could iterate and build up the string manually, but I was hoping there's an existing method for this.
-
benui over 12 yearsYes. I'll reformat it to JS to make it more standard for people to help out with.
-
-
Marcel M. over 12 yearsparts.query will be an Object if url.parse(..., true) is sent. nodejs.org/docs/v0.4.12/api/url.html#url.parse
-
Alex Turpin over 12 yearsIt's not a bug, it's clearly documented: nodejs.org/docs/latest/api/url.html
-
ampersand over 12 yearsyou're right. This will work if url.parse(req.url) is used, without the last parameter
-
Marcel M. over 12 yearsYou're right, but isn't the latest version unstable? I only checked documentation for 0.4.12
-
Alex Turpin over 12 yearsIndeed, but the documentation has improved, and luckily for us, in this case, is consistent with older versions.
-
Anirudha Gupta almost 12 yearsIs is still querystring or changed to require('qs')
-
Marcel M. almost 12 years
-
Benjamin Gruenbaum about 11 yearsThis is a good answer, I would consider anyone using it to not use the delete operator as it is really slow.
-
Alex Turpin about 11 yearsPrime example of premature optimization there buddy
-
Benjamin Gruenbaum about 11 yearsI did not ask you to change the answer, I left a comment. delete is 1000% slower than setting to null or undefined and makes no sense in objects anyway (what does it even mean to remove an object's property, that makes no sense in OO design). In my opinion, setting it to undefined makes much more sense, it still has the property, it is just not defined any more.
-
Alex Turpin about 11 yearsSorry, didn't mean to come off as aggressive. You are right, if someone is doing something where speed is critical, they might not want to use
delete
. Thank you for your comment. -
Gyuri almost 8 yearsYou can always just set it to undefined. Note, that setting to null will throw an error in this case. Also note, that the docs for the url module are hard to read and strangely there are no examples. ;) Note #3: see stackoverflow.com/questions/14967535/… for 'delete' vs 'assign it to undefined' :)
-
andras almost 5 yearsPrefer
delete
oversetting it to undefined
. If later on you will want to copy the object, undefined will overwrite the existing one, like this{...{search: "searching"}, ...{search: undefined}}
-
auerbachb over 3 yearsusing delete throws a linter error for me stackoverflow.com/questions/63702057 my use case works fine setting the object to undefined.