You can use JavaScript's encodeURIComponent:
encodeURIComponent('select * from table where i()')
giving
'select%20*%20from%20table%20where%20i()'
Answer from Joe on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURIComponent
encodeURIComponent() - JavaScript - MDN Web Docs
The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters).
Top answer 1 of 6
825
You can use JavaScript's encodeURIComponent:
encodeURIComponent('select * from table where i()')
giving
'select%20*%20from%20table%20where%20i()'
2 of 6
155
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()'
encodeURIComponent in nodejs 22 cuts $ sign instead of escaping
Version v22.15.1 Platform Linux localhost.localdomain 6.17.6-1-default #1 SMP PREEMPT_DYNAMIC Wed Oct 29 17:21:06 UTC 2025 (9e452b6) x86_64 x86_64 x86_64 GNU/Linux Subsystem No response What steps ... More on github.com
querystring result not consistent with encodeURIComponent
There was an error while loading. Please reload this page More on github.com
node.js - NodeJS decodeURIComponent not working properly - Stack Overflow
I'm just leaving this here, because I had the same problem. I was using the encodeURIcomponent(str) function in the client and in Nodejs when I did decodeURI(str) had the same problem. More on stackoverflow.com
doc: decodeURIComponent and encodeURIComponent missing
📗 API Reference Docs Problem Version: ✍️ v10.19.0 Platform: ✍️ Linux strawberry 5.4.0-sabayon #1 SMP Fri May 8 19:23:19 UTC 2020 x86_64 Intel(R) Core(TM) i5-5300U CPU @ 2.30GHz GenuineIntel GNU/Lin... More on github.com
W3Schools
w3schools.com › jsref › jsref_encodeuricomponent.asp
JavaScript encodeURIComponent() Method
The encodeURIComponent() method encodes special characters including: , / ?
Attacomsian
attacomsian.com › blog › nodejs-encode-decode-url
How to encode or decode a URL in Node.js
October 3, 2022 - const baseUrl = 'http://example.com/search?q=' const query = 'SELECT * from users WHERE id = 1' // Encode query string const encodedQuery = encodeURIComponent(query) // Build full URL const url = baseUrl + encodedQuery // Print full URL console.log(url) // http://example.com/search?q=SELECT * from users WHERE id = 1
Node.js
nodejs.org › api › querystring.html
Query string | Node.js v26.5.0 Documentation
encodeURIComponent <Function> The function to use when converting URL-unsafe characters to percent-encoding in the query string.
GitHub
github.com › node-modules › urlencode
GitHub - node-modules/urlencode: encodeURIComponent with charset · GitHub
Starred by 143 users
Forked by 27 users
Languages TypeScript 68.6% | JavaScript 31.4%
Dot Net Perls
dotnetperls.com › encodeuri-js
Node.js - encodeURI Use - Dot Net Perls
August 3, 2025 - Tip With encodeURIComponent, we convert valid URL characters to their encoded representation for use in a component (not a full URL).
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript - MDN Web Docs
encodeURI, as the name implies, is used to encode a URL as a whole, assuming it is already well-formed. If you want to dynamically assemble string values into a URL, you probably want to use encodeURIComponent() on each dynamic segment instead, to avoid URL syntax characters in unwanted places.
npm
npmjs.com › package › urlencode
urlencode - npm
October 28, 2023 - encodeURIComponent with charset. Latest version: 2.0.0, last published: 3 years ago. Start using urlencode in your project by running `npm i urlencode`. There are 627 other projects in the npm registry using urlencode.
» npm install urlencode
Published Oct 28, 2023
Version 2.0.0
GitHub
github.com › nodejs › node › issues › 5309
querystring result not consistent with encodeURIComponent · Issue #5309 · nodejs/node
February 18, 2016 - const x = { valueOf: function() { return 'other result'; }, toString: function() { return 'spec result'; } } encodeURIComponent(x) !== require('querystring').escape(x)
Author nodejs
Top answer 1 of 3
8
I cannot reproduce it in 0.10 or 0.11 versions of node.
You can convert first to second using new Buffer('Ulysses Guimarães - lado par', 'binary').toString('utf8'), but it's a workaround, not a solution.
Are you sure you're calling decodeURI, not unescape?
2 of 3
5
Use var querystring = require("querystring");
The querystring.unescape() method performs decoding of URL percent-encoded characters on the given str.
and then querystring.unescape(str) as per docs:
https://nodejs.org/api/querystring.html#querystring_querystring_unescape_str
Uchicago
contest-server.cs.uchicago.edu › ref › JavaScript › developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURIComponent.html
encodeURIComponent()
April 25, 2019 - The encodeURIComponent() function encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed ...
GitHub
github.com › nodejs › node › issues › 33799
doc: decodeURIComponent and encodeURIComponent missing · Issue #33799 · nodejs/node
June 8, 2020 - https://nodejs.org/api/globals.html · Concise explanation of what you found to be problematic · ✍️ Recently I learned over a Pull Request, that the Node.js documentation lacks mentioning of decodeURIComponent() and encodeURIComponent(). According to browser-compat-data, the support was added in v0.1.100 and v0.1.100.
Author nodejs