简体   繁体   中英

Splitting url parameter node.js?

I am having the following url parameter

sample value actual value contains so many parameters

var data = "browserName=MSIE&cookies=Rgn=%7CCode%3DMUMBAI%7Ctext%3DMumbai%7C; NREUM=s=1376355292394&r=220970&p=2080092;cs_uuid=209712058942163; cs_si=209712058942163.1&javascriptEnabled=true";

Following function is used to get the particular parameter

//Generic Function to get particular parameter

getParameterValue : function(data, parameter) {

    var value = null;
    if (data.length > 0) {
        var paramArray = data.split("&");

        for ( var i = 0; len = paramArray.length, i < len; i++) {
            var param = paramArray[i];
            var splitter = parameter + "=";
            if (param.indexOf(splitter) > -1) {
                value = param.substring(param.indexOf(splitter)
                        + splitter.length, param.length);
                break;
            }

        }
    }
    return value;

}

Example

 getParameterValue(data, "browserName");

 output is MSIE //correct

Problem is

getParameterValue(data, "cookies");

Output is

Rgn=%7CCode%3DMUMBAI%7Ctext%3DMumbai%7C; NREUM=s=1376355292394

But required output is

Rgn=%7CCode%3DMUMBAI%7Ctext%3DMumbai%7C; NREUM=s=1376355292394&r=220970&p=2080092;cs_uuid=209712058942163; cs_si=209712058942163.1

To Know :

1.URL parameter is encoded(clientside) while sending to node server and decoded.

2.NREUM is not encoded, So getParameterValue method splits upto 1376355292394.

Any help to improve getParameterValue function.

Ready to explain more.

Well, getParameterValue() does seem to be parsing data correctly, but data is not encoded properly to distinguish between delimiters and value characters.

The value of cookies , for example:

cookies=Rgn=%7CCode%3DMUMBAI%7Ctext%3DMumbai%7C; NREUM=s=1376355292394&r=220970&p=2080092;cs_uuid=209712058942163; cs_si=209712058942163.1

Should itself be encoded (on top of any encoding used for the cookie values themselves).

cookies=Rgn%3D%257CCode%253DMUMBAI%257Ctext%253DMumbai%257C%3B%20NREUM%3Ds%3D1376355292394%26r%3D220970%26p%3D2080092%3Bcs_uuid%3D209712058942163%3B%20cs_si%3D209712058942163.1

And, it's rather late to try to " fix " this server-side because of the ambiguity. It'll need to be encoded client-side, which can be done with encodeURIComponent() .

'cookies=' + encodeURIComponent(cookies)

If you happen to be using jQuery, you can use jQuery.param() to help ensure proper encoding.

var data = {
    browserName: 'MSIE',
    cookies: document.cookie,
    javascriptEnabled: true
};

data = $.param(data); // browserName=MSIE&cookies=Rgn%3D%257CCode...

Or, you can use a light-weight recreation, at least for the " traditional " format.

function urlParam(params) {
    var res = [];

    for (var name in params) {
        res.push(encodeURIComponent(name) + '=' + encodeURIComponent(params[name]));
    }

    return res.join('&');
}

var data = urlParam(data);

Example: http://jsfiddle.net/GQpTB/


Also, are you aware that Node.js has querystring.parse() ?

// with: var qs = require('querystring');
var query = qs.parse(data);

console.log(query.cookies); // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM