title: NextJS API – Middleware using Dynamic Routes [...]
body: <p>I've found severals way of doing middleware with Nextjs eg:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/51426252/nextjs-route-middleware-for-authentication">nextjs route middleware for authentication</a> (extending using _app.js)</li>
  <li><a href="https://github.com/senchalabs/connect" rel="nofollow noreferrer">https://github.com/senchalabs/connect</a> (writting middleware in express server layer)</li>
  </ul>
  
  <p>&nbsp;</p>
  
  <hr>
  
  <p>&nbsp;</p>
  
  <p>But may I know is there a possible way to do middleware using <a href="https://nextjs.org/docs/api-routes/dynamic-api-routes" rel="nofollow noreferrer">Dynamic API Routes methods</a>?</p>
  
  <p>eg: </p>
  
  <ol>
  <li>add a file on <code>pages/api/auth/[...middleware].js</code></li>
  <li>all the request from <code>pages/api/auth/*</code> will handled by <code>[...middleware].js</code></li>
  </ol>
  
  <p>Recently I came across with a cool nextjs auth package: (<a href="https://next-auth.js.org/getting-started/example" rel="nofollow noreferrer">https://next-auth.js.org/getting-started/example</a>)</p>
  
  <p><a href="https://i.stack.imgur.com/wJV9A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wJV9A.png" alt="enter image description here"></a></p>
  
  <p>I've studied the source and try with below codes on <code>pages/api/[...middleware].js</code></p>
  
  <pre><code>export default async (req, res) =&gt; {
    return new Promise(async resolve =&gt; {
      const done = resolve;
  
      console.log('Auth Middleware....')
      return done();
    })
  }
  </code></pre>
  
  <p>but when i try to fetch the url:</p>
  
  <p><strong><a href="http://localhost:3000/api/auth/user" rel="nofollow noreferrer">http://localhost:3000/api/auth/user</a></strong></p>
  
  <p>The terminal/console doesn't output the console of 'Auth Middleware....'</p>