简体   繁体   English

如何获取Meteor的HTTP.call(或get)方法来修改HTTP请求标头?

[英]How can I get Meteor's HTTP.call (or get) method to modify the HTTP request Headers?

I am trying to speed up my Meteor application by only loading enough content of a webpage to get the <head> tag of its HTML, to obtain its title, image, and description. 我试图加速我的Meteor应用程序,只加载足够的网页内容以获取其HTML的<head>标记,以获取其标题,图像和描述。 I have a client calling a server-side method with the following code: 我有一个客户端使用以下代码调用服务器端方法:

Meteor.call("metaGetter", url, function(err, res){...});

And on the server side, in the metaGetter method, I am using Meteor's HTTP.call: 在服务器端,在metaGetter方法中,我正在使用Meteor的HTTP.call:

var result = HTTP.call('GET', url, {headers: {'content-range': "bytes 0-100"}});

as written in Meteor's documentation. 正如Meteor的文档中所写。 I am able to get the result's content, html. 我能够得到结果的内容,html。 However, after printing the returned headers, I do not see the content-range attribute that I have tried to set. 但是,在打印返回的标题后,我没有看到我尝试设置的content-range属性。

Edit: Akshat's solution works, but only for some websites, very few in fact. 编辑:Akshat的解决方案有效,但仅限于某些网站,实际上很少。 Any help would be much appreciated. 任何帮助将非常感激。

use the range header: 使用range标题:

var result = HTTP.call('GET', url, {headers: {'range': "bytes=0-100"}});

The response should have a content-range header if the server used supports content ranges. 如果使用的服务器支持内容范围,则响应应具有content-range标头。

Of course, this needs a host that supports request ranges. 当然,这需要支持请求范围的主机。 I've tried the above code and it does work on http://www.microsoft.com as the url . 我已经尝试了上面的代码,它确实在http://www.microsoft.com作为url

Its sad to say there's nothing you can do really for websites that don't support it besides requesting the entire document. 它伤心地说没有什么你可以不支持它除了要求整个文档的网站做的确实。

One rather weird alternative is to manually request the webpage as a socket and cut off when you get more bytes than what you need. 一个相当奇怪的选择是手动请求网页作为套接字,并在获得比您需要的更多字节时切断。

In general, you can't have fixed limit if you want always fetch the title: 通常,如果您想要总是获取标题,则不能有固定限制:

  1. Some HTTP servers doesn't support range header: How can I find out whether a server supports the Range header? 某些HTTP服务器不支持范围标头: 如何确定服务器是否支持Range标头?
  2. You can't guarantee that X bytes will always contain title. 您无法保证X字节始终包含标题。 Eg it may appear after 1000 bytes. 例如,它可能出现在1000字节之后。

In general I would fetch whole HTML file. 一般来说,我会获取整个HTML文件。 On most decent servers, that should take less than 100 ms. 在大多数体面的服务器上,这应该不到100毫秒。 Hardly noticeable by human. 人类几乎无法察觉。 If you do that a lot, you may want to allow executing server side method in parallel (see http://docs.meteor.com/#/full/method_unblock ) 如果你这么做,你可能希望允许并行执行服务器端方法(参见http://docs.meteor.com/#/full/method_unblock

If optimization is must, you can use previous method, fetch 100 bytes, but if you don't find </title> than you fall back to downloading whole HTML file. 如果必须进行优化,则可以使用以前的方法,获取100个字节,但如果找不到</title> ,则回退到下载整个HTML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM