从 Node.js 发出网络请求时如何指定网络接口? [英] How to specify a network interface when making net requests from Node.js?
问题描述
在 http.request
或 net.connect
中,是否有可以指定网络接口来启动连接的选项?
In either http.request
or net.connect
, is there an option that I can specify a network interface to initiate a connection?
操作系统级别的 AFAIK 我可以指定地址级别,或路由表中的负载平衡.但是我的软件中界面选择的方式不止这些,我想知道我是否可以在代码中做到这一点.
AFAIK in OS level I can specify address level, or load balancing into routing tables. But the way of interface choosing in my software is more than that, I wanna know if I can do that in codes.
推荐答案
Node 内置了这个:
Node has this built in:
http://nodejs.org/api/net.html#net_net_connect_options_connectionlistener
http://nodejs.org/api/http.html#http_http_request_options_callback
查看localAddress
,只需将其设置为您要使用的接口的IP.
See localAddress
, just set that to the IP of the interface you'd like to use.
这篇关于从 Node.js 发出网络请求时如何指定网络接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!