Last Updated: February 25, 2016
·
19.3K
· mattcodez

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') === '');