How do I URl encode something in Node.js?


Question

I want to URL encode this:

SELECT name FROM user WHERE uid = me() 

Do I have to download a module for this? I already have the request module.

1
276
7/1/2011 11:09:55 PM

Accepted Answer

You can use JavaScript's encodeURIComponent:

encodeURIComponent('select * from table where i()')
537
2/11/2016 9:01:55 PM

The built-in module querystring is what you're looking for:

var querystring = require("querystring");
var result = querystring.stringify({query: "SELECT name FROM user WHERE uid = me()"});
console.log(result);
#prints 'query=SELECT%20name%20FROM%20user%20WHERE%20uid%20%3D%20me()'

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon