简体   繁体   English

快递限制上传速度

[英]Express limit upload speed

Is it possible to limit the upload speed of a file in express?是否可以限制快递文件的上传速度?

Example: The user has 10Mbp/s of internet and I want to limit then to only upload 1/10 of him internet speed.示例:用户有 10Mbp/s 的互联网,我想限制只能上传他互联网速度的 1/10。

I attempted to use the module throttle as this post says https://stackoverflow.com/a/32340972/13539397 but doesn't seem to work.我试图使用模块油门,因为这篇文章说https://stackoverflow.com/a/32340972/13539397但似乎不起作用。

My code:我的代码:

const Throttle = require("throttle");

route.put("/api/upload", (req, res, next) => {
    req.pipe(new Throttle(1)).pipe(fs.createWriteStream(join(__dirname, "./file.png")));
})

Use the package express-throttle-bandwidth instead改用express-throttle-bandwidth

var express = require('express')
var throttle = require('express-throttle-bandwidth');
var app = express()
app.use(throttle(100000)); // limits to 100000 bps

app.put("/api/upload", (req, res, next) => {
    req.pipe(fs.createWriteStream(join(__dirname, "./file.png"));
})

Since this limits the connection of the instance, use this instance only for file upload由于这限制了实例的连接,因此仅将此实例用于文件上传

You can also use express-throttle你也可以使用express-throttle

var express = require("express");
var throttle = require("express-throttle");
  
var app = express();
  
// Allow 5 requests at any rate with an average rate of 5/s
app.put("/api/upload",throttle({ "rate": "5/s" }), (req, res, next) => {
    req.pipe(fs.createWriteStream(join(__dirname, "./file.png"));
})

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

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