Silly express

This commit is contained in:
R. Tyler Croy 2018-09-16 14:39:21 -07:00
parent 4eb7e5fb1b
commit 4c8eb6783a
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
---
layout: post
title: The single most important middleware for Express
tags:
- javascript
- opinion
---
Almost every web framework I have used in the past five years shares the same
stupid flaw: mishandling of redundant slashes. Invariably this causes problems
when some script _somewhere_ joins URL segments together with multiple slashes
in them, and ends up receiving a 404.
The following [ExpressJS](https://expressjs.com/) middleware will properly neutralize redundant slashes
before the routing layer takes over.
```
/*
* Remove redundant slashes in the URL for properly routing
*
* For example: //authentication -> /authentication which ensures that the
* request is routed correctly
*/
app.all('*', (request, response, next) => {
request.url = request.url.replace(/\/+/, '/');
next();
});
```