在 Node.js 中发送 HTTP 请求

Node.js 有一个 内置的 HTTP 库 ,可以让您在没有外部模块的情况下发出 HTTP 请求。 唯一的缺点是 API 有点过时:它依赖于流,不支持 Promise。以下是如何发出 HTTP 请求 httpbin.org 使用 Nodejs 的 http 模块:

<code class="language-javascript">const http = require(http);
// `res` is an instance of Nodes built-in `ServerResponse` class:
// <a href="https://nodejs.org/api/http.html#http_class_http_serverresponse">https://nodejs.org/api/http.html#http_class_http_serverresponse</a>
const res = await new Promise(resolve => {
http.get(<a href="http://httpbin.org/get?answer=42,">http://httpbin.org/get?answer=42,</a> resolve);
});
// A ServerResponse is a readable stream, so you need to use the
// stream-to-promise pattern to use it with async/await.
let data = await new Promise((resolve, reject) => {
let data = ;
res.on(data, chunk => data += chunk);
res.on(error, err => reject(err));
res.on(end, () => resolve(data));
});
data = JSON.parse(data);
data.args; // { answer: 42 }</code>
<code class="language-javascript">const http = require(http);

// `res` is an instance of Nodes built-in `ServerResponse` class:
// <a href="https://nodejs.org/api/http.html#http_class_http_serverresponse">https://nodejs.org/api/http.html#http_class_http_serverresponse</a>
const res = await new Promise(resolve => {
  http.get(<a href="http://httpbin.org/get?answer=42,">http://httpbin.org/get?answer=42,</a> resolve);
});

// A ServerResponse is a readable stream, so you need to use the
// stream-to-promise pattern to use it with async/await.
let data = await new Promise((resolve, reject) => {
  let data = ;
  res.on(data, chunk => data += chunk);
  res.on(error, err => reject(err));
  res.on(end, () => resolve(data));
});

data = JSON.parse(data);
data.args; // { answer: 42 }</code>
const http = require(http);
// `res` is an instance of Nodes built-in `ServerResponse` class:
// <a href="https://nodejs.org/api/http.html#http_class_http_serverresponse">https://nodejs.org/api/http.html#http_class_http_serverresponse</a>
const res = await new Promise(resolve => {
http.get(<a href="http://httpbin.org/get?answer=42,">http://httpbin.org/get?answer=42,</a> resolve);
});
// A ServerResponse is a readable stream, so you need to use the
// stream-to-promise pattern to use it with async/await.
let data = await new Promise((resolve, reject) => {
let data = ;
res.on(data, chunk => data += chunk);
res.on(error, err => reject(err));
res.on(end, () => resolve(data));
});
data = JSON.parse(data);
data.args; // { answer: 42 }
const http = require(http);

// `res` is an instance of Nodes built-in `ServerResponse` class:
// <a href="https://nodejs.org/api/http.html#http_class_http_serverresponse">https://nodejs.org/api/http.html#http_class_http_serverresponse</a>
const res = await new Promise(resolve => {
  http.get(<a href="http://httpbin.org/get?answer=42,">http://httpbin.org/get?answer=42,</a> resolve);
});

// A ServerResponse is a readable stream, so you need to use the
// stream-to-promise pattern to use it with async/await.
let data = await new Promise((resolve, reject) => {
  let data = ;
  res.on(data, chunk => data += chunk);
  res.on(error, err => reject(err));
  res.on(end, () => resolve(data));
});

data = JSON.parse(data);
data.args; // { answer: 42 }
const http = require(http); // `res` is an instance of Nodes built-in `ServerResponse` class: // https://nodejs.org/api/http.html#http_class_http_serverresponse const res = await new Promise(resolve => { http.get(http://httpbin.org/get?answer=42, resolve); }); // A ServerResponse is a readable stream, so you need to use the // stream-to-promise pattern to use it with async/await. let data = await new Promise((resolve, reject) => { let data = ; res.on(data, chunk => data += chunk); res.on(error, err => reject(err)); res.on(end, () => resolve(data)); }); data = JSON.parse(data); data.args; // { answer: 42 }

您需要了解 Node 的 HTTP 库的一些细微差别:

  1. 支持 https:// 网址。 你会得到一个 Protocol https: not supported. Expected http: 如果你调用出错 http.request() 带有 HTTPS URL。 你需要使用 require(https).request() 反而。
  2. http.request() 具有非标准 回调 签名:没有错误参数。 只是 http.request(url, function callback(res) {}),由于这个非标准的回调签名,你不能使用 http.request()_ promisify() 功能

备择方案

由于 API 中存在这些粗糙的边缘,大多数开发人员不会使用 Node.js 的 HTTP 库来发出请求。 我们建议改用 Axios 。下面是如何使用 Axios 发出上述 HTTP 请求。

<code class="language-javascript">const axios = require(axios);
const res = await axios.get(<a href="https://httpbin.org/get?answer=42);">https://httpbin.org/get?answer=42);</a>
res.data.args; // { answer: 42 }</code>
<code class="language-javascript">const axios = require(axios);

const res = await axios.get(<a href="https://httpbin.org/get?answer=42);">https://httpbin.org/get?answer=42);</a>

res.data.args; // { answer: 42 }</code>
const axios = require(axios);
const res = await axios.get(<a href="https://httpbin.org/get?answer=42);">https://httpbin.org/get?answer=42);</a>
res.data.args; // { answer: 42 }
const axios = require(axios);

const res = await axios.get(<a href="https://httpbin.org/get?answer=42);">https://httpbin.org/get?answer=42);</a>

res.data.args; // { answer: 42 }
const axios = require(axios); const res = await axios.get(https://httpbin.org/get?answer=42); res.data.args; // { answer: 42 }

Axios 使 HTTP 请求比使用 Node.js 的内置库更干净。 另外由于 Axios 请求是 Promise,您可以 使用以下方法处理错误 catch()

© 版权声明
THE END
喜欢就支持一下吧
点赞726 分享
A good idea without action is worth nothing.
如果没有切实执行,再好的点子也是徒劳
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容