Epidemics of infectious disease have been a problem for much of human history in which large portions of a population died. Given that Middle Earth has a level of sanitation similar to that of Medieval Europe in which epidemics were a major threat, has Middle Earth ever encountered an epidemic?
Yes, definitely. From LotR:
Thus began the Shire-reckoning, for the year of the crossing of the Brandywine (as the Hobbits turned the name) became Year One of the Shire... There for a thousand years they were little troubled by wars, and they prospered and multiplied after the Dark Plague (S.R. 37)
and, in an appendix:
In the days of Argeleb II the plague came into Eriador from the Southeast, and most of the people of Cardolan perished, especially in Minhiriath. The Hobbits and all other peoples suffered greatly, but the plague lessened as it passed northwards, and the northern parts of Arthedain were little affected.
Kings of Gondor:
Aldamir (second son of Eldacar) †1540, Hyarmendacil II (Vinyarion) 1621, Minardil †1634, Telemnar †1636. Telemnar and all his children perished in the plague;
The second and greatest evil came upon Gondor in the reign of Telemnar, the twenty-sixth king, whose father Minardil, son of Eldacar, was slain at Pelargir by the Corsairs of Umbar. (They were led by Angamaitë and Sangahyando, the great-grandsons of Castamir.) Soon after a deadly plague came with dark winds out of the East The King and all his children died, and great numbers of the people of Gondor, especially those that lived in Osgiliath.