Checking if a parameter exists with ExpressJS
Express seems like the defacto tool for web routes in NodeJS these days. I had a situation where I wanted to detect the presence of a parameter in a GET URL.
Example:
/api/collection?var
I wanted to know if the parameter is there, even if it has no value. A little trial and error later I learn req.param('var')
will return an empty string if the parameter has no value. If the parameter with that name doesn't exist at all, it will return undefined
. So, simple test:
var getVar = (req.param('var') === '');
Written by mattcodez
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Expressjs
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#