本文介绍了是否有“要求"?喜欢使用http2的软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用请求.现在,我们可以访问http2以便我们的服务器发送服务器请求.

We are using request at work. We now have access to http2 for our server to server requests.

您知道与使用http2的请求具有相同api(ish)的任何节点包吗?

Do you know any node package with the same api (ish) as request that uses http2 ?

推荐答案

我遇到了同样的问题,找不到模块. 这不是一个很好的解决方案,但是您可以修改 request 模块以添加http2.由于没有人能提供更好的答案,因此值得一提.

I faced the same problem and couldn't find a module. This isn't a good solution but you can hack the request module to add http2. As no one has chipped in with a better answer it's worth a mention.

这就是我所做的:

将http2作为布尔值添加到传递给 request 的options对象中.

Add http2 as a boolean to the options object passed into request.

在node_modules/request中运行:

In node_modules/request run:

npm install http2

打开node_modules/request/request.js

Open node_modules/request/request.js

在顶部附近的某个地方需要http2模块:

Require the http2 module somewhere near the top:

, http2 = require('http2')

确保 request 不添加"host"标头(在HTTP2中已弃用,并且如果另一端正在使用http2节点模块,则会导致错误),因此将第304行(或类似内容)更改为:

Make sure request doesn't add the "host" header (deprecated in HTTP2 and causes an error if the other end is using the http2 node module) so change line 304 (or so) to:

if (!self.hasHeader('host') && !options.http2) {

将http2模块添加到第468行的对象声明中(或如此),使其变为:

Add the http2 module to an object declaration on line 468 (or so) so it becomes:

, defaultModules = {'http:':http, 'https:':https, 'http2': http2}

然后使 request 在上一行的变量声明之后使用新模块.将此添加到第471行(左右):

Then make request use the new module after the variable declaration on the previous line. Add this to line 471 (or so):

  if (options.http2) {
    protocol = 'http2';
  }

如果未设置options.http2,则应用程序的其余部分可以保持不变.那么 request 的行为与往常一样.

The rest of the application can remain unchanged, if options.http2 is not set then request behaves like it always does.

在一项非常不科学的测试中,我发现进行此更改将一项服务的吞吐量提高了约25%.

In a very unscientific test I found that making this change increased throughput to one service by about 25%.

这篇关于是否有“要求"?喜欢使用http2的软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 12:51