© 2024 borui. All rights reserved.
This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article:
borui(2024-03-14 03:35:26 +0000). how to assign port for http request in nodejs. https://borui/blog/2024-03-14-en-assign-nodejs-request-port.
@misc{
borui2024,
author = {borui},
title = {how to assign port for http request in nodejs},
year = {2024},
publisher = {borui's blog},
journal = {borui's blog},
url={https://borui/blog/2024-03-14-en-assign-nodejs-request-port}
}
intro
Because I want to do test on a function which sends out request within, and I wondered if I can set up a server during the test.
Futher more, I realized that I don't need a server to send out a request, so I also wondered what would be the source port of that request.
will the outgoing request and incoming request use same port
📓 Note: Technically you can, but it is not the common practice.
Technically you can, and some protocols do (isakmp), but it's not very flexible. Therefore, it is not the common practice.
There are two drawbacks:
You can have only one data stream. A typical web browser makes several tcp connections to a web server to increase performance. That's why you often see the text appear before the graphics on a web page. you can't do that with only one stream.
It's possible for a host to be both a client and a server at the same time. If the port numbers are the same, you can't do that either. Ultimately, using well-known ports for destination and ephemeral ports for source gives the most flexibility.
- Ron Trunk. (answered Jul 13, 2016 at 11:10). You can, and some protocols do (isakmp), but it's not very flexible. There are two drawbacks: You can have only one data stream. A typical web browser makes several tcp connections to a web server to increase performance. That's why you often see the text appear before the graphics on a web page. you can't do that with only one stream.. [Answer]. stackoverflow. https://networkengineering.stackexchange.com/questions/33061/why-cant-a-single-port-be-used-for-both-incoming-and-outgoing-traffic/33063#33063
how is the source port determined in nodejs
It’s the kernel that implicitly provides a random port for sending out requests, not nodejs.
- Henningsen. [@addaleax]. (Jan 4, 2021). Will libuv get hold of a random port, then trigger the http request via this port and still use epoll api? It’s the kernel that implicitly provides a random port (not fully random, though – usually in the range 49152–65535, to avoid conflicts with actual servers), not libuv.does axios library internally invoke libuv api?It uses the built-in http module, which in turn uses the net.Socket API (or the libuv API directly) for communication. Once a socket is established, there’s no difference on the I/O level between a server socket and a client socket.Also, does nodejs handle all the file handling operations asynchronously by picking threads from libuv threadpool? Yes.is epoll api applicable only for network calls? It is usable for most libuv features, including network calls, and most non-filesystem I/O.. [Reply]. github. https://github.com/nodejs/help/issues/3160#issuecomment-754295674
Take a step further this is also a networking porblem
If the system is behind NAT, a port on NAT server will be randonly allocated and bound with the port randomly allocated by the system itself.
The NAT is going to decide/determine the outbound port for a NATed connection/session, via it's own internal means. Meaning, it will vary according to the implementation of the NAT. This means any responses back will come back to that same outbound port.
- Brad. (answered Mar 29, 2013 at 18:01). The NAT is going to decide/determine the outbound port for a NATed connection/session, via it's own internal means. Meaning, it will vary according to the implementation of the NAT. This means any responses back will come back to that same outbound port. As for your question: What happens, for example, if two computers in a LAN send HTTP requests with the same source port to the same website?It will assign different outbound ports for each. Thus, it can distinguish between the two in responses it receives. A NATs would create/maintain a mapping of translated ports, creating new outbound port numbers for new sessions. So even if if there were two different "internal" sessions, from two different machines, on the same port number, it would map to two different port numbers on the outgoing side. Thus, when packets came back in on the respective ports, it would know how to translate them back to the correct address/port on the inside LAN.. [Answer]. stackoverflow. https://stackoverflow.com/questions/15708011/how-is-source-port-for-http-determined-is-there-ever-collision-in-nat/15708801#15708801
how to bind a source port for the reqeust
When your Node.js server sends an HTTP request to an external server (e.g., an API or another web service), the source port used by your server is typically randomly assigned by the operating system. However, if you want to bind a specific source port to your request, you can follow these approaches:
-
Explicitly Specify a Local Port:
- When creating an HTTP client (e.g., using
http
oraxios
), you can explicitly specify a local port in the request options. - Use the
localPort
property to configure the source port. - Example using
http
module:const http = require('http'); const options = { hostname: 'example.com', port: 80, localPort: 52000, // Specify your desired local port method: 'GET', }; const req = http.request(options, (res) => { // Handle the response }); req.end();
- When creating an HTTP client (e.g., using
-
Access the Socket Properties:
- Node.js HTTP requests expose the underlying socket.
- You can use the
localAddress
andlocalPort
properties of the socket to bind to a specific local address and port. - Example:
const http = require('http'); const req = http.request('http://example.com', (res) => { // Handle the response }); req.on('socket', (socket) => { socket.localPort = 52000; // Set your desired local port }); req.end();
-
Custom HTTP Requester with Basic Sockets:
- If you need more control, you can write your own HTTP requester using basic Node.js sockets.
- This allows you to directly manipulate the socket properties.
- Example (low-level approach):
const net = require('net'); const client = new net.Socket(); client.connect(80, 'example.com', () => { client.localPort = 52000; // Set your desired local port client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'); }); client.on('data', (data) => { // Handle the response client.end(); });
Remember that binding to specific ports can be useful for scenarios like testing, debugging, or working with specific network configurations. Choose the approach that best fits your use case! 🚀
For more details, explore the Node.js documentation on HTTP.¹²³
Source: Conversation with Bing, 2024年3月14日 (1) node.js - Specify client port Node js request - Stack Overflow. https://stackoverflow.com/questions/51454221/specify-client-port-node-js-request. (2) How to run Node/Express on a specific port? - GeeksforGeeks. https://www.geeksforgeeks.org/how-to-run-node-express-on-a-specific-port/. (3) Is there a way to set the source port for a node js https request?. https://stackoverflow.com/questions/36554680/is-there-a-way-to-set-the-source-port-for-a-node-js-https-request. (4) Bind your node.js/express apps to localhost - Joe Flateau. https://joeflateau.net/posts/bind-your-node-js-express-apps-to-localhost. (5) Specify port number in HTTP request (node.js) - Stack Overflow. https://stackoverflow.com/questions/31662411/specify-port-number-in-http-request-node-js. (6) undefined. http://example.com. (7) How does Node.js choose random ports? - Stack Overflow. https://stackoverflow.com/questions/9901043/how-does-node-js-choose-random-ports. (8) node.js - Nodejs random free tcp ports - Stack Overflow. https://stackoverflow.com/questions/28050171/nodejs-random-free-tcp-ports. (9) Assigning a Random Port in Node - Brian Childress. https://www.brianchildress.co/assign-random-port-in-node/. (10) Assign a Random Port to Node.js Server - Maxim Orlov. https://maximorlov.com/tips/assign-a-random-port-to-nodejs-server/. (11) HTTP | Node.js v21.7.1 Documentation. https://nodejs.org/api/http.html. (12) undefined. https://github.com/joyent/node/blob/v0.6.11/lib/net.js. (13) node.js - Specify local port for TCP socket client connection - Stack .... https://stackoverflow.com/questions/25642583/specify-local-port-for-tcp-socket-client-connection.
The feature appears to be undocumented, but you can achieve this by setting BOTH the localAddress and localPort parameters for the options argument in https.request.
For more information, see the code here: https://github.com/nodejs/node/blob/b85a50b6da5bbd7e9c8902a13dfbe1a142fd786a/lib/net.js#L916
A basic example follows:
var https = require('https');
var options = {
hostname: 'example.com',
port: 8443,
localAddress : '192.168.0.1',
localPort: 8444
};
var req = https.request(options, function(res) {
console.log(res);
});
req.end();
- binarymax. (answered Apr 11, 2016 at 18:23). The feature appears to be undocumented, but you can achieve this by setting BOTH the localAddress and localPort parameters for the options argument in https.request.. [Answer]. stackoverflow. https://stackoverflow.com/questions/36554680/is-there-a-way-to-set-the-source-port-for-a-node-js-https-request/36556341#36556341