How do I URl encode something in Node.js?
Use the escape
function of querystring
. It generates a URL safe string.
var escaped_str = require('querystring').escape('Photo on 30-11-12 at 8.09 AM #2.jpg');
console.log(escaped_str);
// prints 'Photo%20on%2030-11-12%20at%208.09%20AM%20%232.jpg'
You can use JavaScript's encodeURIComponent
:
encodeURIComponent('select * from table where i()')
giving
'select%20*%20from%20table%20where%20i()'
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()'