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.
The Evil Breath
The "Evil Breath" was an epidemic that caused many children to get sick and die towards the end of the First Age.
‘Ever will some new evil be hatched in Angband beyond the guess of Elves and Men,’ they said. And in the autumn of that year, to point their words, there came an ill wind from the North under leaden skies. The Evil Breath it was called, for it was pestilent; and many sickened and died in the fall of the year in the northern lands that bordered on the Anfauglith, and they were for the most part the children or the rising youth in the houses of Men.
The Children of Húrin - Chapter 1 - "The Childhood of Túrin"
Túrin and his sister get it, and although Túrin recovers, his sister dies.
But before the year was out the truth of his father’s words was shown; for the Evil Breath came to Dor-lómin, and Túrin took sick, and lay long in a fever and dark dream. And when he was healed, for such was his fate and the strength of life that was in him, he asked for Lalaith. But his nurse answered: ‘Speak no more of Lalaith, son of Húrin; but of your sister Urwen you must ask tidings of your mother.’
And when Morwen came to him, Túrin said to her: ‘I am no longer sick, and I wish to see Urwen; but why must I not say Lalaith any more?’
‘Because Urwen is dead, and laughter is stilled in this house,’ she answered. ‘But you live, son of Morwen; and so does the Enemy who has done this to us.’
The Children of Húrin - Chapter 1 - "The Childhood of Túrin"
The Great Plague
Around mid-way through the third age, their was a new epidemic known as "the Great Plague" or "the Dark Plague". This led to the decline and depopulation of Gondor that we see in The Lord of the Rings a few hundred years later, and to the watch on Mordor being let down enough for Sauron to reestablish himself
The waning of the Northmen of Rhovanion began with the Great Plague, which appeared there in the winter of the year 1635 and soon spread to Gondor. In Gondor the mortality was great especially among those who dwelt in cities. It was greater in Rhovanion, for though its people lived mostly in the open and had no great cities, the Plague came with a cold winter when horses and men were driven into shelter and their low wooden houses and stables were thronged; moreover they were little skilled in the arts of healing and medicine, of which much was still known in Gondor, preserved from the wisdom of Númenor. When the Plague passed it is said that more than half of the foil of Rhovanion had perished, and of their horses also.
Unfinished Tales - "Cirion and Eorl and the Friendship of Gondor and Rohan"
In the days of Argeleb II the plague came into Eriador from the South-east, 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. It was at this time that an end came of the Dúnedain of Cardolan, and evil spirits out of Angmar and Rhudaur entered into the deserted mounds and dwelt there.
The Lord of the Rings - Appendix A I iii - "Eriador, Arnor, and the Heirs of Isildur"
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. Then for weariness and fewness of men the watch on the borders of Mordor ceased and the fortresses that guarded the passes were unmanned.
The Lord of the Rings - Appendix A I iv - "Gondor and the Heirs of Anárion"
1636: The Great Plague devastates Gondor. Death of King Telemnar and his children. The White Tree dies in Minas Anor. The plague spreads north and west, and many parts of Eriador become desolate. Beyond the Baranduin the Periannath survive, but suffer great loss.
The Lord of the Rings - Appendix B - "The Tale of Years"
And in the days of Telemnar, the third and twentieth of the line of Meneldil, a plague came upon dark winds out of the east, and it smote the King and his children, and many of the people of Gondor perished. Then the forts on the borders of Mordor were deserted, and Minas Ithil was emptied of its people; and evil entered again into the Black Land secretly, and the ashes of Gorgoroth were stirred as by a cold wind, for dark shapes gathered there.
The Silmarillion - "Of the Rings of Power and the Third Age"
The Dunlendings suffered, like all the peoples of Arnor and Gondor, in the Great Plague of the years 1636-7 of the Third Age, but less than most, since they dwelt apart and had few dealings with other men.
Unfinished Tales - "The Battles of the Fords of Isen"
In The Atlas of Middle-earth, Karen Wynn Fonstad has put together a map showing the effect of the Great Plague.