Node.js url.parse result back to string

44,128

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 of query
  • query (object; see querystring) will only be used if search 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);
Share:
44,128

Related videos on Youtube

benui
Author by

benui

Unreal Engine UI programmer working at Brace Yourself Games. I share UI-related UE4 tutorials on my website.

Updated on July 09, 2022

Comments

  • benui
    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
      benui over 12 years
      Yes. I'll reformat it to JS to make it more standard for people to help out with.
  • Marcel M.
    Marcel M. over 12 years
    parts.query will be an Object if url.parse(..., true) is sent. nodejs.org/docs/v0.4.12/api/url.html#url.parse
  • Alex Turpin
    Alex Turpin over 12 years
    It's not a bug, it's clearly documented: nodejs.org/docs/latest/api/url.html
  • ampersand
    ampersand over 12 years
    you're right. This will work if url.parse(req.url) is used, without the last parameter
  • Marcel M.
    Marcel M. over 12 years
    You're right, but isn't the latest version unstable? I only checked documentation for 0.4.12
  • Alex Turpin
    Alex Turpin over 12 years
    Indeed, but the documentation has improved, and luckily for us, in this case, is consistent with older versions.
  • Anirudha Gupta
    Anirudha Gupta almost 12 years
    Is is still querystring or changed to require('qs')
  • Marcel M.
    Marcel M. almost 12 years
    qs if you want visionmedia's query string parser, querystring for nodejs native parser.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum about 11 years
    This is a good answer, I would consider anyone using it to not use the delete operator as it is really slow.
  • Alex Turpin
    Alex Turpin about 11 years
    Prime example of premature optimization there buddy
  • Benjamin Gruenbaum
    Benjamin Gruenbaum about 11 years
    I 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
    Alex Turpin about 11 years
    Sorry, 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
    Gyuri almost 8 years
    You 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
    andras almost 5 years
    Prefer delete over setting 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
    auerbachb over 3 years
    using delete throws a linter error for me stackoverflow.com/questions/63702057 my use case works fine setting the object to undefined.