When using Express' URL parameter functionality, it seems that parameters are automatically decoded. That is, percent-encoded entities are resolved to their normal form. %20
is replaced with a space.
However, a plus +
is not replaced with a space. This is presumably because Express is using decodeURIComponent()
internally, which also does not replace plus +
with a space. Simple example code:
app.get('/:sourceFile', function (req, res, next) {
console.log(req.params.sourceFile);
});
If you request /test%20test
, then you get test test
on the console. If you request /test+test
, then you get test+test
on the console.
Is there a way to change this mode of operation in Express 4? Is this a bug?
You are trying to use + to represent a space in the "URI part" of your request. You can't do that. A plus sign is translated to a space only in query strings.
It is not a bug. In URI specs (page 12/13 https://www.rfc-editor.org/rfc/rfc3986), plus sign is a reserved character, not meant to be translated as a space.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With