Update middleware.js

Fix "Cannot read property '1' of null" bug