Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

node.js - NodeJS Express encodes the URL - how to decode

I'm using NodeJS with Express, and when I use foreign characters in the URL, they automatically get encoded.

How do I decode it back to the original string?

Before calling NodeJS, I escape characters.

So the string: ?????

Becomes %u05D0%u05D5%u05D1%u05DE%u05D4

The entire URL now looks like: http://localhost:32323/?query=%u05D0%u05D5%u05D1%u05DE%u05D4

Now in my NodeJS, I get the escaped string %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 url and querystring libraries but nothing seems to fit my case.

querystring.unescape(query); // still '%u05D0%u05D5%u05D1%u05DE%u05D4'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update 16/03/18

escape and unescape are deprecated.

Use:
encodeURIComponent('?????') // %D7%90%D7%95%D7%91%D7%9E%D7%94
decodeURIComponent('%D7%90%D7%95%D7%91%D7%9E%D7%94') // ?????

Old answer

unescape('%u05D0%u05D5%u05D1%u05DE%u05D4') gives "?????"

Try:

var querystring = unescape(query);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...