【问题标题】:Nodejs - Get remote file contents using custom network InterfaceNodejs - 使用自定义网络接口获取远程文件内容
【发布时间】:2016-05-07 05:51:19
【问题描述】:

我有一台分配了整个 ip 块的机器。 我想调用一个远程 URL 来获取它的内容。 我希望能够选择呼叫来自哪个接口/IP 地址。

例如。 我有 IP 地址 8.8.8.8 和 8.8.8.9 远程文件是“http://www.google.com/robots.txt” 另一个远程文件是“http://www.yahoo.com/robots.txt

我希望通过我的 ip 8.8.8.8 调用第一个 url google.com/robots.txt 第二个网址 yahoo.com/robots.txt 通过我的 ip 8.8.8.9

我正在使用 request.get 方法。

【问题讨论】:

    标签: node.js interface ip


    【解决方案1】:

    当你在 node 中初始化一个 http 请求时,你可以使用 localaddress 设置接口:https://nodejs.org/api/http.html#http_http_request_options_callback

      var options = {
        localaddress: '8.8.8.8',
        hostname: 'www.google.com',
        port: 80,
        path: '/upload',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': postData.length
        }
        var callback = function (res, err) { ...do something... }
        http.request(options, callback)
    

    【讨论】:

    • 如果您对答案感到满意,那么最好将其标记为正确答案。 ;)
    • 我做到了。它说我需要大约 15 的声誉才能公开
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多