简体   繁体   中英

Specify client port Node js request

Sending a request from NodeJs, I want to specify the starting port, for example:
request to google.com port 80
from port 52000.
So the connection will be from from port 52000 to 80. My goal is to bootstrap a web server under a NAT, but I'm not allowed to set the forwarding port on the NAT itself.
How can I perform it? Thank you!!

In the official node documentation (which I advise you to check), you have a localAddress and localPort properties, which you can use to bind to them before connecting to a remote socket.

That's the lowest level. Node HTTP requests expose the socket, so you can use it to access those properties, or you can write your own HTTP requester with basic nodejs sockets.

Sounds like you want to change the outbound port. That port is chosen internally by the NAT and therefore there is no control over it from node.js .

If you want to request google.com:80 you can choose the port 52000 internally for your machine. But the NAT will take 52000 and map it to some other port based upon some internal algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM