20

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.

0

4 Answers 4

53

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This is a good answer, I would consider anyone using it to not use the delete operator as it is really slow.
Prime example of premature optimization there buddy
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.
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.
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' :)
|
3

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));

5 Comments

It's not a bug, it's clearly documented: nodejs.org/docs/latest/api/url.html
You're right, but isn't the latest version unstable? I only checked documentation for 0.4.12
Indeed, but the documentation has improved, and luckily for us, in this case, is consistent with older versions.
Is is still querystring or changed to require('qs')
qs if you want visionmedia's query string parser, querystring for nodejs native parser.
2

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);

2 Comments

parts.query will be an Object if url.parse(..., true) is sent. nodejs.org/docs/v0.4.12/api/url.html#url.parse
you're right. This will work if url.parse(req.url) is used, without the last parameter
0

To dry out code and get at URL variables without needing to require('url') I used:

/* 
   Used the url module to parse and place the parameters into req.urlparams.
   Follows the same pattern used for swagger API path variables that load 
   into the req.params scope.

*/
  app.use(function(req, res, next) {
    var url = require('url');
    var queryURL = url.parse(req.url, true);
    req.urlparams = queryURL.query;
    next();
  });

var myID = req.urlparams.myID;

This will parse and move the url variables into the req.urlparams variable. It runs early in the request workflow so is available for all expressjs paths.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.