【问题标题】:Cancel a file download/request on node.js http.Client based on file size根据文件大小取消 node.js http.Client 上的文件下载/请求
【发布时间】:2011-06-17 11:17:34
【问题描述】:

我在 node.js 上创建了一个函数来开始文件下载,但我想创建一个规则,让该函数在下载数据之前检查文件大小。

我得到了响应标头并检查了大小,但我不知道如何在不传输实际数据/正文的情况下取消所有内容。也许有一种方法可以先只传输标头,如果符合我的规则,我可以触发另一个请求来进行下载。

这是我的代码片段:

request.on('response', function(response) {
        var filesize = response.headers['content-length'];
        console.log("File size " + filename + ": " + filesize + " bytes.");
        response.pause();
        if (filesize >= 50000) {
            // WHAT TO PUT HERE TO CANCEL THE DOWNLOAD?
            console.log("Download cancelled. File too big.");
        } else {
            response.resume();
        }
        //Create file and write the data chunks to it

谢谢。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    根据HTTP协议规范9.4 HEAD

    HEAD 方法与 GET 相同 除了服务器不能返回 响应中的消息正文。这 HTTP 中包含的元信息 响应 HEAD 请求的标头 应该与信息相同 为响应 GET 请求而发送。 该方法可用于获取 关于实体的元信息 由请求暗示而没有 转移实体主体本身。 这种方法常用于测试 有效性的超文本链接, 可访问性和最近 修改。

    对 HEAD 请求的响应可能是 在某种意义上是可缓存的 响应中包含的信息 可用于更新以前的 来自该资源的缓存实体。如果 新的字段值表明 缓存的实体与当前的不同 实体(如将由 Content-Length、Content-MD5、 ETag 或 Last-Modified),然后是缓存 必须将缓存条目视为陈旧的。

    如果您的服务器对此没有正确响应,我认为您可能不走运? 接下来只需使用google.request('HEAD' 而不是google.request('GET'


    一些代码

    我在下面测试了以下内容。 fake.js 只是一个使用 express 测试的假服务器。

    fake.js:

    var HOST = 'localhost';
    var PORT = 3000;
    var connections = 0;
    var express = require('express');
    var app = module.exports = express.createServer();
    
    if (process.argv[2] && process.argv[3]) {
        HOST = process.argv[2];
        PORT = process.argv[3];
    }
    
    app.use(express.staticProvider(__dirname + '/public'));
    
    // to reconnect.
    app.get('/small', function(req,  res) {
        console.log(req.method);
        if (req.method == 'HEAD') {
            console.log('here');
            res.send('');
        } else {
            connections++;
            res.send('small');    
        }
    });
    
    app.get('/count', function(req, res) {
        res.send('' + connections);
    });
    
    app.get('/reset', function(req, res) {
        connections = 0;
        res.send('reset');
    });
    
    
    if (!module.parent) {
        app.listen(PORT, HOST);
        console.log("Express server listening on port %d", app.address().port)
    }
    

    test.js 是从 http-client 测试 head。 test.js:

    var http = require('http');
    var google = http.createClient(3000, 'localhost');
    var request = google.request('HEAD', '/small',
      {'host': 'localhost'});
    request.end();
    request.on('response', function (response) {
      console.log('STATUS: ' + response.statusCode);
      console.log('HEADERS: ' + JSON.stringify(response.headers));
      response.setEncoding('utf8');
    });
    

    alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
    0
    

    alfred@alfred-laptop:~/node/stackoverflow/4832362$ node test.js 
    STATUS: 200
    HEADERS: {"content-type":"text/html; charset=utf-8","content-length":"0","connection":"close"}
    

    alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
    0
    

    如您所见,仍为 0。

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2018-10-04
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多