【问题标题】:CURL --resolve in node.jsCURL --resolve 在 node.js
【发布时间】:2021-09-14 07:38:27
【问题描述】:

有人可以提供 cURL --resolve 等效于 NodeJS 中的以下请求-

curl http://www.example.com --resolve www.example.com:80:127.0.0.1

输出:

cURL 在终端中工作,但我需要使用 NodeJS 为我的应用程序执行类似的请求。

关注的文章:CURL --resolve in node.js axios 但这也给了我 404 Not Found 错误:

Debugger attached.
(node:33104) UnhandledPromiseRejectionWarning: Error: Request failed with status code 404
    at createError (/home/anish/Desktop/SampleWeb/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/anish/Desktop/SampleWeb/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/home/anish/Desktop/SampleWeb/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:33104) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:33104) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...

我尝试过其他 stackoverflow 问题,但没有一个解决方案看起来很有希望。

【问题讨论】:

    标签: node.js axios


    【解决方案1】:

    以任何语言正确执行此操作(它甚至可以在不是您编写的代码中使用,例如 Google Chrome 或 Microsoft Edge)。编辑你的主机文件。

    在 Linux/Unix/Mac 上编辑 /etc/hosts..

    在 Windows 上编辑 c:\Windows\System32\Drivers\etc\hosts

    然后添加这一行:

    127.0.0.1    www.example.com
    

    现在,当您 curl www.example.com 或使用 node.js 访问它或将 Google Chrome 指向 www.example.com 时,您将获得 127.0.0.1

    【讨论】:

      【解决方案2】:

      Node 有一个“代理”的概念,它是一个可以传递给 Axios、node-fetch 或 Node 内置的 HTTPS.request() 的对象。其目的是允许您控制网络请求的发出方式。

      以下方法不起作用:

      import axios from 'axios';
      const spoof = 'www.example.com';
      // This could also be a hostname like 'example.org':
      const ip = '127.0.0.1';
       
      axios.get(`https://${ip}`).then((body) => {
          console.log(body.data);
      }).catch((error) => {
          console.error(error);
      });
      

      虽然这会向127.0.0.1 发出请求,但在那里运行的服务器不会知道您在请求www.example.com。您需要在两个位置指定:

      1. Host 标头中。
      2. 如果使用 HTTPS,在代理的 servername 字段中,用于 SNI。 SNI 是一个值,它向服务器指示它应该提供什么 HTTPS 证书,并在 SSL/TLS 设置阶段在 HTTP 流量之前发送。

      通常,Node 会获取域名(在这种情况下是变量'ip'的值)并将其作为 SNI 值发送(实际上,如果是 IP 地址,它应该发送'') .

      按如下方式进行:

      import axios from 'axios';
      import { Agent } from 'https';
      const spoof = 'www.example.com';
      const ip = '127.0.0.1';
           
      const agent = new Agent({
          servername: spoof,
      });
      axios.get(`https://${ip}`, {
          headers: {
              Host: spoof,
          },
          httpsAgent: agent,
      }).then((body) => {
          console.log(body.data);
      }).catch((error) => {
          console.error(error);
      });
      

      如果您不使用 HTTPS,那么在这种情况下您不需要提供代理。


      编辑: 我提到代理适用于其他 HTTP 请求库,以说明:node-fetch 中如何执行相同操作的示例:

      fetch(`https://${ip}`, {
        headers: { Host: spoof },
        agent: new Agent({ servername: spoof }),
      }).then(...)
      

      【讨论】:

        猜你喜欢
        • 2020-07-22
        • 2016-10-31
        • 2021-10-01
        • 2019-02-08
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 2017-03-30
        • 2011-11-21
        相关资源
        最近更新 更多