updated middleware to process the response returned by the app