An impoverished Mexican town nearly flattened by a 2017 earthquake has welcomed thousands of tired and hungry Central American migrants in quiet defiance of Donald Trump’s condemnation of the group.

As Trump ordered 5,200 troops to the US-Mexico border to block the migrant caravan, residents of the southern town of Niltepec – who still live among piles of rubble that once were their homes – welcomed the caravan with homemade soup, medical tents, and diapers for children.

“We wish we had a space dignified enough to offer our visitors,” said Zelfareli Cruz Medina, Niltepec’s mayor.

As she spoke, caravan members were stringing up garbage bags to use as tents in Niltepec’s main square. Surrounding buildings were scarred with cracks and gaping holes caused by the 8.2 magnitude earthquake that struck the region on 7 September 2017.

Of 1,720 homes in Niltepec, 1,602 were damaged in the quake, according to town officials, while 530 collapsed entirely. At least 100 families are still without homes, they said.

A tower atop Niltepec’s main church was stripped down to its fragile wood skeleton by the quake and Cruz said the town needed help to rebuild its library and the mayor’s offices, which were serving as a shelter for the caravan’s women and children.

The US president has seized upon the migrant caravan ahead of midterm elections, claiming without evidence that it includes “gang members and very bad people”.

Migrants in the group say that they are fleeing poverty and violence. They are still 2,000 miles by road and weeks away from reaching a US port of entry, where most are expected to seek asylum as the law allows.

But a willingness to help the needy comes as almost second-nature to residents of the hardscrabble town in Oaxaca, one of Mexico’s poorest states, Cruz said. “We know now what it means to suffer,” she said.

Near the church, three local families gathered at lunchtime in the shared backyard of rebuilt homes to cook an offering of chicken soup for the migrants.

Later, Mariela Escobar, 52, a part-time cleaner, pored over a vat of steaming tamales – “hundreds of them”, she said – to hand out free for dinner.

“People helped us greatly,” said her neighbor, Angela Moreno Galves, 81. “So now, of course, we want to help too.”

Setting out from Honduras on 13 October, the caravan quickly swelled to number several thousand people. The latest estimates put its size at 3,500 to more than double that – matching or exceeding Niltepec’s population of 3,800.

On Tuesday, more than 1,000 people in a second caravan that forged its way across the river from Guatemala began walking through southern Mexico and reached the city of Tapachula.

But Mexican federal officials seem intent only on seeing the caravan melt away as it moves through the country. The government regularly reports the number of migrants who have applied for refugee status or agreed for assisted bus trips back to their home countries.

The warmth of the welcome in Niltepec stood in deliberate, stark contrast to Trump’s hostility, said Jorge Luis Fuentes, a senior town official.

“It’s a form of struggle,” he said. “It’s a way to demonstrate that rights are universal.”