node.js NodeJS Express 对 URL 进行编码 - 如何解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34576659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS Express encodes the URL - how to decode
提问by AlexD
I'm using NodeJS with Express, and when I use foreign characters in the URL, they automatically get encoded.
我在 Express 中使用 NodeJS,当我在 URL 中使用外来字符时,它们会自动进行编码。
How do I decode it back to the original string?
如何将其解码回原始字符串?
Before calling NodeJS, I escape characters.
在调用 NodeJS 之前,我会转义字符。
So the string: ?????
所以字符串: ?????
Becomes %u05D0%u05D5%u05D1%u05DE%u05D4
成为 %u05D0%u05D5%u05D1%u05DE%u05D4
The entire URL now looks like: http://localhost:32323/?query=%u05D0%u05D5%u05D1%u05DE%u05D4
整个 URL 现在看起来像: http://localhost:32323/?query=%u05D0%u05D5%u05D1%u05DE%u05D4
Now in my NodeJS, I get the escaped string %u05D0%u05D5%u05D1%u05DE%u05D4.
现在在我的 NodeJS 中,我得到了转义字符串%u05D0%u05D5%u05D1%u05DE%u05D4。
This is the relevant code:
这是相关代码:
var url_parts = url.parse(req.url, true);
var params = url_parts.query;
var query = params.query; // '%u05D0%u05D5%u05D1%u05DE%u05D4'
I've tried urland querystringlibraries but nothing seems to fit my case.
我试过url和querystring图书馆,但似乎没有什么适合我的情况。
querystring.unescape(query); // still '%u05D0%u05D5%u05D1%u05DE%u05D4'
回答by eckymad
Update 16/03/18
更新 16/03/18
escapeand unescapeare deprecated.
escape并unescape已弃用。
Use:encodeURIComponent('?????') // %D7%90%D7%95%D7%91%D7%9E%D7%94decodeURIComponent('%D7%90%D7%95%D7%91%D7%9E%D7%94') // ?????
用:encodeURIComponent('?????') // %D7%90%D7%95%D7%91%D7%9E%D7%94decodeURIComponent('%D7%90%D7%95%D7%91%D7%9E%D7%94') // ?????
Old answer
旧答案
unescape('%u05D0%u05D5%u05D1%u05DE%u05D4')gives "?????"
unescape('%u05D0%u05D5%u05D1%u05DE%u05D4')给 "?????"
Try:
尝试:
var querystring = unescape(query);
var querystring = unescape(query);
回答by AlexD
You should use decodeURI()and encodeURI()to encode/decode a URL with foreign characters.
您应该使用decodeURI()和encodeURI()对带有外来字符的 URL 进行编码/解码。
Usage:
用法:
var query = 'http://google.com';
query = encodeURI(query);
query = decodeURI(query); // http://google.com
Reference on MDN:
MDN上的参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
回答by Mr S Coder
#**update may-2020**
# how to use an encoder/decoder on node js
### I am writing my answer due to I have noisy data I spend 4 hour to fixed
data email input = [email protected]
data URL input = /us/home
```
decodeURI function that only decodes a URL special character
email output =>myemail%40gmail.com
url output => %2Fus%2F
using decodeURIComponent
email output = > [email protected]
url output => /us/
```
here some clarification where you can use decodeURI and decodeURIComponent a fucntion

