I just went into hard mode, I only have like 32 defense and a night's edge, and the pirates invaded almost immediately. The captain is also right at spawn, as well as a horde of all the others, making it impossible to get to my house and survive for more than a second. Is there something I can do to at least survive a little longer?
This happened to me as well and sadly there isn't any easy way out of it. Moving your spawn will help stop you from having to fight pirates as soon as you spawn in but that won't stop the pirate invasion.
It's a long process but even with only the Night's Edge you can kill a few pirates at a time. After you kill enough and the invasion ends you just need to kill the last few left. One thing to remember is not all of the pirates can open doors, so if you ever need a minute to heal you can always run into your house for a little while.
Run like hell.
If you cannot escape, you can consider clicking the bed to unset your spawn point. This will spawn you at the world spawn on your next death, giving you some time. This will work until the pirates find you there too. Other than that, try to survive as best as you can and start preparing for the next time they invade.
I have had that exact same thing happen to me. What I did was took my ballista rod from defeating the old ones army and placed it again over and over. To beat that flying boat thing I trapped it by walking into a building I made, stood at the other end shot it a lot with guns and bows. You shoot at the cannons but do that last because if you beat it another one comes.
You get a ballista rod by buying it from the tavernkeep.
You should first break/turn off your spawn, destroy bed, put the bed in another house below ground (right below your house) and put your bed in there, and make sure it is a house that would allow a NPC to live in there. (chair, table, light source and wood walls) That way pirates can't walk in there OR spawn in there. A Rod of Discord would help get in and out of your spawn place, but it is extremely rare. It's just my preference.
Dropped By Chaos Elemental Drop Rate 0.2% (1:500)
This item cannot be crafted.
The Rod of Discord is a Hardmode tool that instantly teleports the player to the mouse cursor. It has a 1/500 (0.2%) chance of being dropped by Chaos Elementals.
Using the Rod costs no mana or ammunition, however each use inflicts the Chaos State debuff on the player, which causes the player to lose health if they use the Rod in rapid succession. Chaos State lasts for 6 / 9 seconds after each use, and takes away 1/7th / 1/6th of the player's maximum health if the Rod is used again during that period.