title: ExpressJS router normalized/canonical urls
body: <p>I'm after normalized/canonical urls for SPA with ExpressJS server. </p>
  
  <p>Although it is SPA that is backed up by server side router - templates can differ a bit for app urls. One of the differences is <code>&lt;link rel="canonical" href="https://example.com{{ originalPath }}"&gt;</code> tag. Not the relevant detail but explains the context of the question. I expect that there will be only one URL that responds with 200, its variations are redirected to it with 301/302 (works for living humans and search engines).</p>
  
  <p>I would like to make the urls case-sensitive and strict (no extra slash), similarly to <a href="http://expressjs.com/en/4x/api.html#express.router" rel="noreferrer"><code>Router</code></a> options, but non-canonical urls (that differ in case or extra slash) should do 301/302 redirect to canonical url instead of 404.</p>
  
  <p>In most apps I just want to force the urls for <code>*</code> routes to be lower-cased (with the exception of queries), with no extra slashes. I.e. <code>app.all('*', ...)</code>, and the redirects are:</p>
  
  <pre><code>/Foo/Bar/ -&gt; /foo/bar
  /foo/Bar?Baz -&gt; /foo/bar?Baz
  </code></pre>
  
  <p>But there may be exceptions if the routes are defined explicitly. For example, there are camel-cased routes:</p>
  
  <pre><code>possiblyNestedRouter.route('/somePath')...
  possiblyNestedRouter.route('/anotherPath/:Param')...
  </code></pre>
  
  <p>And all non-canonical urls should be redirected to canonical (parameter case is left intact):</p>
  
  <pre><code>/somepath/ -&gt; /somePath
  /anotherpath/FOO -&gt; /anotherPath/FOO
  </code></pre>
  
  <p>The logic behind canonical urls is quite straightforward, so it is strange that I couldn't find anything on this topic regarding ExpressJS.</p>
  
  <p>What is the best way to do this? Are there middlewares already that can help?</p>