Angered by the spectacle of American hostages overseas and prodded by a Democratic desire to shed an antidefense image, the House shifted in the last two weeks into a hard-line mood on defense and foreign policy.

On issues from resumed nerve gas production to a death penalty for peacetime spies in the military, the House showed in its votes on the 1986 defense authorization bill that it was determined to look a little tougher.

Not all votes went that way. For instance, the House voted to halt further deployment of the MX missile and freeze defense spending. But for the most part, as one Capitol Hill aide put it, "It's John Wayne day up here."

In perhaps their most telling vote, lawmakers agreed to give President Reagan the one weapons system they had consistently denied him -- nerve gas. They did it by a more than 30-vote margin that surprised both Republicans and Democrats.

At the same time, the House also voted to:

* Boost spending on Reagan's "Star Wars" space defense system by 80 percent. The amount was less than Reagan wanted but more than a Democratic alternative had provided. The lawmakers also imposed no arms-control restrictions on Star Wars, as some Democrats had wanted.

* Provide an extra $150 million for the Midgetman nuclear missile above the $774.5 million recommended by the House Armed Services Committee. Permit the Pentagon to use lie detector tests to monitor the loyalty of more than 4 million military and civil employes with access to classified information.

* Allow military courts to apply the death penalty against those found guilty of peacetime espionage.

The House also voted to ban U.S. combat troops in Nicaragua, but the prohibition was weakened by several GOP amendments that authorized Reagan to deploy troops without consulting Congress in specific circumstances. These include Nicaraguan acquisition of Soviet-made MiG fighters.

Liberals charged that the House had provided Reagan with a "road map" for U.S. intervention in Nicaragua.

The defense votes occurred just days after the House sharply toughened its stance toward Nicaragua, reversing itself and voting to renew funding for counterrevolutionaries seeking to overthrow Nicaragua's leftist government.

Lawmakers and lobbyists said last week that the votes clearly reflected the frustration many members felt at seeing the United States under siege -- in the hijacking of Trans World Airlines Flight 847, in the killing of four U.S. Marines in El Salvador and in revelations of possible damage to U.S. security by the Walker spy ring.

"The whole debate is influenced . . . by what's happened internationally in the last two weeks," Rep. Leon E. Panetta (D-Calif.) said. "The way you can lash out and feel strong is to vote for this madness," said Rep. Ronald V. Dellums (D-Calif.).

But an equally significant factor was the desire of a key bloc of 50 to 60 mostly moderate Democrats to move the party toward the center on defense. This group includes the man who handled the defense bill on the House floor, House Armed Services Committee Chairman Les Aspin (D-Wis.).

"Since the 1984 election there is concern about the party projection of being soft on defense," a key Democratic official said. "People are reluctant to take on the president." In a House with about 190 members on the right of center and roughly an equal number to the left, this middle bloc was able to hold sway during much of the debate. They also were key to the House reversal on renewing aid to Nicaraguan rebels.

Aspin said, "The moderate Democratic position is what's coming out of all this. Where the Democratic mainstrean is now is moved a little" to the right.

Aspin was made chairman of the Armed Services Committee this year to refurbish the Democrats' defense posture after the Democratic Caucus ousted the aging incumbent, Melvin Price (D-Ill.). With his slightly left-of-center image and respected expertise on defense issues, he was more able to forge alliances across the political spectrum than either his aging and conservative predecessor or other senior members of the generally conservative committee.

Aspin said the House-approved defense bill is "what I think we ought to do on the defense budget, which is vote for some weapons. The Democrats should be for some stuff -- for MX, for binaries nerve gas , for more money for the Midgetman, for more money for conventional weapons.

"We ought to distance ourselves from the left by voting for some weapons, but also from the administration by supporting less money and more arms control," he said.

The moderates also helped in the handful of victories that House liberals felt they got in this year's defense budget: the capping of MX deployment, a freeze on defense spending next year and a curb on further testing of antisatellite weapons.

These wins were significant enough for some liberal Democrats to vote for the defense bill on final passage for the first time.

"It's a major breakthrough to have the president's legs cut off for the first time on a major weapons system," said Rep. Barney Frank (D-Mass.), referring to the MX.

But, several other liberals and arms-control lobbyists pointed out last week that there were elements of the new hard-line mood even in the wins.

The capping of the MX does not shut down the program, but allows an indefinite number of missiles to continue to be produced for operational and testing purposes. Several longtime MX foes had wanted to kill the program. And the antisatellite weapons victory was narrower than last year.

In addition, the spending freeze, like the House votes to crack down on defense contractors and procurement, had as much to do with a bipartisan belief that the familiar villains of waste, fraud and abuse are still afoot in the Pentagon.

"There's two dimensions at work here," said Rep. Dick Cheney (R-Wyo.), a member of the House GOP leadership. "When you talk about procurement, wasteful spending there's a mood of being tough on defense. In terms of U.S. force in the world, it seems that that's moved in the president's direction this year. The Vietnam syndrome is not as pronounced in the past."