Reputation: 1796
What is the best way to deal with case options for the following scenario
i want to be able to check if the req.query String rowCount, rowcount or ROWCOUNT exists and then get the value of it. hope there is a simple way to check if the querystring in any type of case exists.
if i use
if (!req.query.rowcount) {var t_rowcount = parseInt(25)}
it will not match if the querystring is rowCount
fyi i am running NodeJs on a windows 2016 server
Upvotes: 1
Views: 903
Reputation: 1796
With some help of rakesh answer i was able to come up with a solution which does what i need.
app.use(function(req, res, next) {
for (const key in req.query) {
if( (key.toLowerCase() === key ) == false){
req.query[key.toLowerCase()] = req.query[key]
delete req.query[key]
}
}
this middleware actually gets all keys in the req.query object, checks if the key is all lowercase, if it is we don't touch it. If not we create new key with the lowercase key and afterwards delete the original non lowercase key so the object size stays same.
Also if someone need this in upper or camel case this can be done by simply changing tolowerCase out
Upvotes: 2
Reputation: 338
t's not possible as-is, but you could insert a very simple middleware which would, for instance, lowercase all keys in req.query:
// insert this before your routes
app.use(function(req, res, next) {
for (var key in req.query)
{
req.query[key.toLowerCase()] = req.query[key];
}
next();
});
Upvotes: 1