Expressjs / connect - 模拟慢速文件上传

时间:2012-10-16 16:39:18

标签: node.js connect express jquery-html5uploader

有没有办法在connect或express中设置模式或值 模拟慢速文件上传??

1 个答案:

答案 0 :(得分:1)

首先,安装node-limiter然后创建一个Transform流,以阻止任何Stream

var util = require('util');
var Transform = require('stream').Transform;
var TokenBucket = require('limiter').TokenBucket;

function BucketStream(rate, interval, parentBucket, options) {
    Transform.call(this, options);
    this.bucket = new TokenBucket(Number.POSITIVE_INFINITY, rate, interval, parentBucket);
}

util.inherits(BucketStream, Transform);

BucketStream.prototype._transform = function(chunk, encoding, callback) {
    this.bucket.removeTokens(chunk.length, function(err) {
        callback(err, chunk);
    });
};

然后请求是ReadableStream

var bucketStream = new BucketStream(1024 * 500, 'second'); // 500KB/sec
req.pipe(bucketStream);

现在从bucketStream(而不是req)读取,您只需要500KB /秒

我做到了这一点,所以要小心:)