how to not remove the (-, ) sign if it's in the beginning of the string in node js get query
const time_zone = req.query.time_zone
result: " 02:00"
supposed to be: " 02:00"
CodePudding user response:
The
character is used to escape spaces in the query part of the URL. That's why
new URL("http://server/?time_zone= 02:00").searchParams.get("time_zone")
gives ' 02:00'
(note the leading space). And req.query.time_zone
is determined in this way. The -
character has no such special treatment.
Strictly speaking, the client should call the URL http://server/?time_zone=+02:00
, which gives req.query.time_zone = ' 02:00'
, but I suggest to take this burden away from the client by defining
var time_zone = req.query.time_zone.replace(" ", " ");
which works with both variants.