Update middleware.js

optimize code