title: Express middleware calls multiple times
body: <p>I've checked other posts related to this topic and couldn't find the problem in my code. </p>
  
  <pre><code>const myMiddleware = (fn) =&gt; {
      return (req, res, next) =&gt; {
          var fullUrl = req.protocol + '://' + req.get('host') + req.url;
          console.log(fullUrl)
          next()
      }
  }
  
  const app = express()
  
  app.use('/dist', express.static(__dirname + '/client/dist'))
  app.use('/static', express.static(__dirname + '/client/static'))
  
  app.use(bodyParser.urlencoded({ extended: false }))
  app.use(cookieParserMiddleware())
  app.use(passport.initialize())
  
  const server = https.createServer(options, app)
  
  app.get('/', myMiddleware((req, res) =&gt; {
      res.sendFile(__dirname + '/client/dist/index.html')
  }))
  
  app.all('*', (req, res) =&gt; {
      res.redirect('/')
  })
  
  server.listen(8080, function listening() {
      console.log('Listening on %d', server.address().port)
  })
  </code></pre>
  
  <p>Without the <code>myMiddleware</code> on <code>'/'</code> path everything works as expected. With <code>myMiddleware</code> attached as <code>app.get('/', myMiddleware((req, res) =&gt; {</code> then <code>myMiddleware</code> is called multiple times without <code>res.sendFile(__dirname + '/client/dist/index.html')</code> being called.</p>
  
  <hr>
  
  <p>EDIT: The bug below is fixed with jfriend00's solution. The middleware getting called multiple times still exists. The reason is favicon and some other assets are not being captured by <code>app.use('/static', express.static(__dirname + '/client/static'))</code> line. Fixing path solved the first bug as well</p>
  
  <p>On top of that problem, I've tried removing the part below but then the app doesn't work at all. My guess is there are 2 bugs here.</p>
  
  <pre><code>app.all('*', (req, res) =&gt; {
      res.redirect('/')
  })
  </code></pre>
  
  <p>I have posted an image on how it looks when removing <code>app.all('*'..)</code></p>
  
  <p><a href="https://i.stack.imgur.com/6a9mu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6a9mu.png" alt="enter image description here"></a></p>