I went through a phase where I fainted when blood was taken. It was strange because I'd been having blood taken regularly for years with no problem, and after a while I stopped fainting when having blood taken, with no reason why I should have gotten over the problem either.
During my fainting phase, what helped me most was to lie down while having blood drawn rather than sit up. Due to gravity, your blood stays nearer your head when lying down so it's less likely you'll get light headed or faint. Also, psychologically I preferred knowing that if I did faint, I'd have nowhere to fall as I was down already, and ironically this knowledge calmed me so I was less likely to faint then anyway! For the IV for your endoscopy I expect you'll be lying down anyway.
And definitely tell the nurse and anyone else involved that you may faint. It's a common problem and they should know the best ways for managing it regarding this particular test.
Other tips which may help:
Don't look at the IV, but don't shut your eyes either. With your eyes shut you could start imagining mental images of the thing you're trying not to think about. Look at something or someone in the room, or if you're lying down you can look at the ceiling - no needles there!
Memorise a short poem to recite in your head (or out loud if you don't mind the doctors and nurses hearing) to give yourself something to concentrate on. Or recite your times tables - it's hard to be bored and anxious at the same time.
Chat to the nurses - they'll probably chat to you anyway, especially if you've told them you're nervous.
You can probably tell that I've had a lot of procedures that involved potential fainting. Once I'd started doing these tips I stopped fainting and would only get light-headed. Now I usually stay sat up for blood tests, I still don't look at it and I either chat with the nurse or recite something in my head and I'm fine 90% of the time. The only thing that can cause me to faint now is having anything inserted into my bladder. For some reason this is always incredibly painful for me even though it's not supposed to be, and once I began to anticipate the physical pain I would make myself anxious as well, and it became a vicious circle. During my last hospital admission, I had to get over this very quickly as I was having catheters in and out regularly, and I did this in the way whoops described in the post above: when I explained the problem to a nurse, she did something to numb the area, and when I realised I was no longer feeling the extreme physical pain (it was still painful, but bearable) I was able to relax more. So then it works the other way - what's the opposite of a vicious circle? A virtuous circle?