Fixed middleware to return 501 for unknown methods