有没有办法在connect或express中设置模式或值 模拟慢速文件上传??
答案 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 /秒
我做到了这一点,所以要小心:)