title: How to catch error of specific router request before it ends up in global error handler
body: <p>So my routes are ordered like shown bellow</p>
  
  <p>verifyToken middleware is being used in a lot of routes.
  Generally if an error occurs i want the global error handler of index.js to handle it.</p>
  
  <p>But if the error occurred while verifyToken middleware is being used by the login.html route with method = get i would like
  to handle it inside routers/user.js which i thought i could do by using <code>router.get(/\/login(\.html)?$/, (error, req, res, next) =&gt; {}</code> but the error bypasses it and moves to global error handler.</p>
  
  <p>index.js</p>
  
  <pre><code>const userRouter = require('./routers/user')
  
  app.get('', (req, res) =&gt; {
      res.render('index')
  })
  
  app.use(userRouter)
  
  app.use((req, res, next) =&gt; {
      res.status(404).redirect('/404.html');
  })
  
  //Global error handling
  app.use( (error, req, res, next) =&gt; {
   switch(error.name) {
          case "UnauthorizedError":
              console.log("UnauthorizedError = ", error.message)
              res.status(401).redirect('/401.html');
              break
          case "InternalServerError":
              console.log("InternalServerError = ", error.message)
              res.status(500).send('whatever')
              break
          default:
              console.log("Another error = ", error)
      }
  })
  </code></pre>
  
  <p>/routers/user.js</p>
  
  <pre><code>const verifyToken = require('../middleware/authentication/verifyToken')
  
  router.get(/\/login(\.html)?$/, verifyToken, (req, res) =&gt; {
      // If he is already logged in redirect him to dashboard
      // This route works as expected
      res.redirect('/admin/dashboard.html')
  });
  
  router.get(/\/login(\.html)?$/, (error, req, res, next) =&gt; {
      // If error = Unauthorized
      // which means that he is not logged in proceed
      if(error.name === 'UnauthorizedError') res.render('login')
      // else pass error to global error handler (at index.js)
      else next(error)
  });
  
  module.exports = router
  </code></pre>
  
  <p>/middleware/authentication/verifyToken.js</p>
  
  <pre><code>const jwt = require('jsonwebtoken')
  var createError = require('http-errors')
  
  const verifyToken = async (req, res, next) =&gt; {
      try {
          // Do some stuff
          if (token_doesnt_exist) return next(createError(401, 'TOKEN NOT FOUND', {expose: false}))
          // Do some stuff
          next()
      } catch {
          next(createError(e.status, e.message, {expose: false}))
      }
  })
  
  module.exports = verifyToken
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I ended up transforming</p>
  
  <p><code>router.get(/\/login(\.html)?$/, (error, req, res, next) =&gt; {}</code>  </p>
  
  <p>to </p>
  
  <p><code>router.use((error, req, res, next) =&gt; {}</code></p>
  
  <p>which i guess works since it only catches errors from the above route.
  I'm not sure if this is the best way i'd really like to see an alternative.</p>
  
  <pre><code>const verifyToken = require('../middleware/authentication/verifyToken')
  
  router.get(/\/login(\.html)?$/, verifyToken, (req, res) =&gt; {
      // If he is already logged in redirect him to dashboard
      // This route works as expected
      res.redirect('/admin/dashboard.html')
  });
  
  router.use((error, req, res, next) =&gt; {
      // If error = Unauthorized
      // which means that he is not logged in proceed
      if(error.name === 'UnauthorizedError') res.render('login')
      // else pass error to global error handler (at index.js)
      else next(error)
  });
  
  module.exports = router
  </code></pre>