0

I format a string (I can format the string in any way)

e.g

   id1:234,id2:4566,id3:3000

then encrypt it and pass via query string

   ?qs=djdjdjfjf57577574h33h3h3hh3h3

then decrypt it on node

   id1:234,id2:4566,id3:3000

which is the best method to format the string and then turn it into an array in node

    arr[id1] = "234";
    arr[id2] = "4566";
    arr[id3] = "3000";
5
  • 1
    can I ask what is the context for this solution? What are you doing with it? Commented May 19, 2014 at 15:38
  • pass argument from php site to a node soket Commented May 19, 2014 at 15:41
  • What are id1, id2 and id3 supposed to be in your example? They are not variables, but string literals right? Commented May 19, 2014 at 15:42
  • yes because I encrypt and decript a string for security,I do not want to pass values ​​that can be haked Commented May 19, 2014 at 15:45
  • 1
    That example is so close to JSON that for me is the obvious next step to achieve what you want. Commented May 19, 2014 at 16:01

2 Answers 2

4

I think you would like to use hashmap (object) instead of array: var obj = {} then try:

"id1:234,id2:4566,id3:3000".split(",").forEach(function(e){
    var record = e.split(":");
    obj[record[0]] = record[1];
})

which give you: {id1: "234", id2: "4566", id3: "3000"}

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

4 Comments

There is no each function. You can use forEach, but this is not supported for <IE8.
It will be on NodeJS side, I assumed there is "each" since on Chrome there is :)
There is no each function as far as I can tell
@Amberlamps edited, thanks (seems to site on which I ran console has that method in array).
3

Firstly, what you have as your object model is more of a hash like object or simply a JavaScript object with keys and values, so the best way to do that is simply using JSON.parse.

So if you can do it as you said: I can format the string in any way, you better first change your formatting to have something like:

'{"id1":234,"id2":4566,"id3":3000}'

instead of :

id1:234,id2:4566,id3:3000

you can do it using JSON.stringify on the client side JavaScript using a JavaScript object without you needing to deal with string formatting:

//on the client side
var myObj = {};
myObj.id1 = 234;
myObj.id2 = 4566;
myObj.id3 = 3000;
var objStr = JSON.stringify(myObj);

let's say you encrypt your string using a function named encrypt:

var encryptedStr = encrypt(objStr);
//so now you should use encodeURI to be able to put it in the queryString
var queryStringParam = encodeURI(encryptedStr);

now you put the queryStringParam in the queryString.

Then on the node.js side, all you should do is parsing it as a JSON object. The other important point that you probably have considered doing it is using decodeURI. For the last step, let's say you are using e function named decrypt:

//the server-side code
var decryptedStr = decrypt(decodeURI(yourQueryString));
var obj = JSON.parse(decryptedStr);

Now obj is exactly what you want.

1 Comment

thanks It works! but if i put the client side part is useless to encrypt the string ;)

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.