WASHINGTON (AP) — The Senate narrowly confirmed Mike Pompeo to be secretary of state on Thursday, clearing the path for him to take over as the top U.S. diplomat just as President Donald Trump faces high-risk moments on Iran and North Korea.

Pompeo, the outgoing CIA director, secured support from 57 senators, with 42 voting no — one of the slimmest margins for the job in recent history. Every past nominee for the job since at least the Carter administration has received 85 or more yes votes in the Senate, with the exception of Trump's first secretary of state, Rex Tillerson, who got 56.

He is expected to be sworn into office immediately and then to depart within hours of the vote for Europe on his first trip as secretary of state.

Pompeo, a former Kansas congressman, is expected to guide Trump's foreign policy in a more right-leaning direction than Tillerson, the former Exxon Mobil CEO fired by Trump on Twitter last month. He inherits a State Department and diplomatic corps that is deeply demoralized after a tumultuous first year under Tillerson, who pushed budget and staff cuts and eschewed public appearances while leaving key diplomatic positions unfilled.

The Senate vote followed an uneasy confirmation process for Pompeo that underscored President Donald Trump's growing difficulties in getting nominees in place for top positions. On Monday, it appeared Pompeo would fail a vote in the Senate Foreign Relations Committee, but the panel ultimately cleared him after last-minute support from Republican Sen. Rand Paul of Kentucky.

A long list of pressing issues awaits him including a decision on the Iran nuclear deal and Trump's upcoming summit with North Korean leader Kim Jong Un.

Previously confirmed by the Senate for the CIA job, Pompeo was supported by all the Republican senators and by six Democrats. The Democrats included several up for re-election in conservative-leaning states, including Heidi Heitkamp of North Dakota and Joe Manchin of West Virginia. GOP Sen. John McCain of Arizona, who is being treated for cancer, was absent.

In the run-up to Pompeo's confirmation, his backers emphasized his resume as a West Point and Harvard Law School graduate and former congressman who enjoys a close relationship with Trump particularly on North Korea. Pompeo, as CIA director, traveled to Pyongyang over Easter after being nominated for secretary of state. In the North Korean capital he met with leader Kim Jong Un ahead of the planned meeting with Trump, expected in late May or June.

"He's the perfect person to come in at this time and lead those efforts" on North Korea, Senate Foreign Relations Committee Chairman Bob Corker, R-Tenn., said on the Senate floor moments before Pompeo was confirmed.

Yet his opponents had warned that his hawkish foreign policy views and negative comments about gay marriage and Muslims made him ill-equipped to serve as a diplomat or to represent the United States on the world stage. Pompeo used his confirmation hearing to try to soften that image, edging away from past comments about regime change in North Korea.

Pompeo will take the helm at the State Department ahead of Trump's expected decision on May 12 about whether to withdraw from the Iran nuclear deal and re-impose sanctions on Tehran. Pompeo, a staunch Iran critic, has long deplored the 2015 nuclear accord, but has supported Trump's efforts to get European allies to strengthen restrictions on Iran.

"If there's no chance that we can fix it, I will recommend to the president that we work with our allies to achieve a better outcome and to achieve a better deal," Pompeo said in his confirmation hearing earlier this month.

Ahead of his confirmation vote, the State Department was making contingency plans for Pompeo to travel to Brussels for a meeting of NATO foreign ministers immediately after his swearing-in, administration officials said. Acting Secretary of State John Sullivan was originally scheduled to take the trip starting Thursday but was expected to defer to Pompeo instead.

From Brussels, Pompeo was expected to travel to the Middle East with stops in Saudi Arabia, Israel and Jordan over the weekend and Monday, said the officials, who were not authorized to publicly discuss the planning and spoke on condition of anonymity.

In Brussels, Pompeo will keep up pressure on NATO's European members, particularly Germany, to boost their defense spending to 2 percent of their gross domestic product by 2024 as they pledged to do at an alliance summit in Wales in 2014, one senior official said. The official singled out Germany, the alliance's largest and wealthiest European member, for not having plans to increase defense spending to more than 1.25 percent of GDP by 2021.

Only six NATO allies currently meet the goal, and only nine allies have produced realistic plans for reaching it by 2024. That spending level, frequently incorrectly referred to by Trump as a contribution to NATO itself, is particularly important given the allies need to combat increased Russian aggression, the official said.

In Riyadh, Jerusalem and Amman, the future of the Iran deal and the conflict in Syria will be significant agenda items. Pompeo will arrive in the region ahead of a series of events that could potentially plunge it into deeper disarray, including the Iran deal decision and the relocation of the U.S. Embassy from Tel Aviv to Jerusalem.

Deeply opposed by the Palestinians, the opening of the Jerusalem embassy is expected only two days after the May 12 deadline on the Iran deal. May 12 is also the date for parliamentary elections in Iraq that will be held amid concerns that pro-Iranian candidates may win, giving Tehran further influence in Middle East.