Home > Blockchain >  NodeJS Express allow handeful requests to skip authentication check
NodeJS Express allow handeful requests to skip authentication check

Time:02-16

Is there a request path regex matcher in NodeJS , basically I would like to build a capability to allow certain paths to skip token validation. How to do it in Express.

function verifyToken(req, res, next) {
  // Get auth header value
  const whitelistPaths = ["/ping","**/swagger-ui.html/**","/health/*"];
  if(req.path matches any of values in whitelist array) , call next()
  const bearerHeader = req.headers["Authorization"];
  Authentication check
router.get("*", verifyToken, apiProxy);

CodePudding user response:

Just use a regex as the path :

router.get((\/ping)|(.*\/swagger-ui\.html)|(\/health\/.*), verifyToken, apiProxy);

this will catch /ping or /swagger-ui.html or /health

here is the docs: https://expressjs.com/en/guide/routing.html

  • Related