0

From node.js documentation

querystring.parse('foo=bar&baz=qux&baz=quux&corge')

// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }

When I parse for the parameters on the server side, is there a way I can check if these parameters are sent in a correct sequence?

For example, I want to enforce that the foo=bar is always the FIRST parameter and so on. How is this achievable?

Right now I can do something like:

var queryData = querystring.parse('foo=bar&baz=qux&baz=quux&corge');

But then doing

if(queryData.foo) {...}

only checks if the parameter 'foo' is present in the url, not if it's in the right place

periphery
  • 75
  • 1
  • 9
  • 2
    Any reason that you want to do this? I always assume the order of querystring won't change anything. – Farid Nouri Neshat May 26 '14 at 04:04
  • Answers below are good, but I agree that this is a bad design choice. – Paul May 26 '14 at 04:18
  • @FaridNouriNeshat: I was thinking of enforcing a protocol to make parsing easier, but it seems node already helps us. I had limited experience coming from a C background in server development. – periphery May 26 '14 at 04:22
  • @Paul: Thanks for your input. Would you mind telling me why it would be a bad design choice? And how I could improve it? – periphery May 26 '14 at 04:22
  • 1
    Well, bad design choice b/c the order of querystring params normally doesn't matter. check the RFC, there's no ordinality requirement. So it's non-standard, and putting semantic importance on something that is normally unimportant is generally going to lead you to bad user experience / errors that you don't need to solve. I'd improve it by just using the querystring as the key-value store it's intended to be. – Paul May 26 '14 at 04:26
  • 1
    Let's assume the people writing the client code know this limitation/requirement(if they don't they will be cursing you!). Imagine the method that they are using to produce the querystring by default will result in a different order(for example a html form with a different order of input elements) and they have to write extra code to fix the order. You are also writing extra code to enforce this. Code is liability, extra code will result in more bugs, and takes more time to develop and test, which means more costly. Simplicity is important. – Farid Nouri Neshat May 26 '14 at 04:33

3 Answers3

3

You can either parse it yourself with something like this:

var qs = 'foo=bar&baz=qux&baz=quux&corge';
var parsed = qs.split('&').map(function (keyValue) {
    return keyValue.split('=');
});

Which will give you something like [ ['foo','bar'], ['baz', 'qux'], ['baz', 'quux'], ['corge', '' ], then you can check if the first is what you want or not: if (parsed[0][0] === 'foo')

You can also check this question for parsing it yourself: How can I get query string values in JavaScript?

Or you can rely on the fact that nodejs will insert the property in the same order it was received and v8 will keep the order(which is not really reliable, they might change the behavior): if (Object.keys(queryData)[0] === 'foo').

Or you can just check if foo is appeared first:

if (qs.indexOf('foo=') === 0)

or something like if (qs.slice(0, 4) === 'foo')

Community
  • 1
  • 1
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
0

You might be better of doing an old fashion string.split since it gives you an array of split elements. You can then inspect the array

How do I split a string, breaking at a particular character?

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
0

check if this code works for you:

var querystring = require("querystring");
var obj =  querystring.parse('foo=bar&baz=qux&baz=quux&corge');

var rankOfObjects = {"0":"foo","1":"baz","2":"corge"};
var objArray = Object.keys(obj);

if(checkRank()){
    console.log("in order");
}else{
    console.log("not in order");
}

function checkRank(){
    for(var rank in objArray){
        if(objArray[rank]!=rankOfObjects[rank]){
             return false;
        }
     }
     return true
}
azero0
  • 2,220
  • 3
  • 20
  • 31