Speed up the upload limit

Can the upload speed of a file in express be limited?

For instance, if a user has 10Mbps internet speed, is it possible to restrict their file upload speed to 1/10 of that?

I tried using the throttle module based on the suggestion in this post , but it did not work for me.

This is the code I used:

const Throttle = require("throttle");

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

Answer №1

For throttling bandwidth in your Express app, consider using the package 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"));
})

This will restrict the connection of the instance, making it suitable for file upload functionality only

If needed, you can also explore express-throttle

var express = require("express");
var throttle = require("express-throttle");
  
var app = express();
  
// Set a limit of 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"));
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The @Input() property within an Angular component is producing an empty array as its result

I have a calendar component that has a data property marked as @Input(): import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-calendar', templateUrl: './calendar.comp ...

Discover the best methods for accessing all pages on a Facebook account

I attempted to retrieve a list of all Facebook pages, but encountered an error. The error message states: 'request is not defined.' Here is the code snippet: var url = 'https://graph.facebook.com/me/accounts'; var accessToken = req.u ...

Including only certain information in a GET request

Essentially, when I send a GET request to my REST API, I only want specific data to be returned. Here is the code for my current GET request: router.get('/user/all', function(req, res, next){ User.find({name: req.query.name}).then(function(as ...

What methods are available for parsing a JSON string that includes functions?

My JSON object has the following structure: string str = '[ { "name": "data-input1", "type": "Element [in]", "description": "data-input1", "getConnectorPosition": "function (element) { return {x: 0, y: Math.floor(element.rectangle.w ...

Leveraging the power of Ajax in JavaScript

For my assignment, I successfully made two separate paragraphs hide and show using jQuery. Moving forward, the next step involves integrating JavaScript Ajax code to allow the paragraphs to toggle visibility. To achieve this, I created two distinct HTML fi ...

Vue.js - Unable to access property 'push' as it is not defined

I am facing an issue with the code below that is giving me the error message Cannot read property 'push' of undefined: var vm = new Vue({ el: '#root', data: { posts: [], newPost: {} }, ...

What is the best way to create a fixed sidebar in Bootstrap?

Is there a way to create a sticky sidebar similar to cpmta.org using Bootstrap? I'm struggling to figure it out... ...

What are the functioning principles of older ajax file uploading frameworks (pre-html5)?

Traditional HTML includes an input element with a file type option. Tools are available that enable asynchronous file uploads with progress tracking. From what I gather, this is achieved by splitting the file into chunks and sending multiple requests wit ...

How can I redirect applications from a web browser to Safari?

Is there a way to redirect applications from a browser to Safari? For Android devices, I have implemented a feature where if a user opens my site through a browser built-in within apps like Facebook or Telegram, they are automatically redirected to Google ...

Determine if the browser displays <select multiple> as a modal dialog

Is it possible to use JavaScript to detect whether a specific browser displays a focused <select multiple> element as a popup or strictly as an inline box? On certain platforms, like the Android Browser and iOS Safari, the appearance of a popup can ...

Guide on implementing mongoskin to display query results on a webpage

Currently, I am utilizing the mongoskin Node.js plugin for communicating with MongoDB. The issue arises from all mongoskin API methods being asynchronous, while my setup involves a synchronous Node.js server (using express) to handle webpage requests. Ho ...

Shifting the processing of JavaScript in the node.js server to the client side

Exploring the impact of transitioning processing tasks that are typically done on the server to being handled by the client in a node.js web application. For example, consider a scenario where a user uploads a CSV file containing a year's worth of ba ...

Error: Document expected to be found has already been removed

Upon attempting to log into the app, I encountered an error message in the client console (chrome dev tools) that reads as follows: Uncaught Error: Expected to find a document already present for removed mongo.js?69942a86515ec397dfd8cbb0a151a0eefdd9560d:2 ...

Tips for correctly linking JS and CSS resources in Node.js/Express

I have a JavaScript file and a stylesheet that I am trying to link in order to use a cipher website that I created. Here is my File Path: website/ (contains app.js/html files and package json) website/public/css (contains CSS files) website/public/scri ...

Creating a multi-dimensional array in the correct format for an AJAX post in JavaScript

Assistance needed with a JavaScript code that must create a multi-dimensional array in a specific format for later transmission via AJAX. In the table provided below, data needs to be collected for selected services by each customer: Client ID will act as ...

employing a particular texture on a personalized shape leads to a WebGL issue stating "trying to reach vertices that are out of bounds."

When implementing the following code: let shape = new THREE.Geometry() shape.vertices.length = 0 shape.faces.length = 0 shape.vertices.push(new THREE.Vector3(0, 0, 0)) shape.vertices.push(new THREE.Vector3(0, 0, 32)) shape.vertices.push(new THREE.Vector3( ...

The loading image display is experiencing technical difficulties

This is the script I have created to implement paging functionality: var loadPage = function () { var $link = $(this); var options = { url: $link.attr("href"), data: $("form").serialize(), type: "get ...

vue.js libs requiring "new ..." are being scolded by eslint for their misspelling

When working with Vue libraries, such as Swiper, that require the use of 'new …' in the script section, I encounter ESlint errors no matter how I try to write it. Even though 'new ...' works perfectly fine in the frontend, ESlint cont ...

Discover the power of utilizing JavaScript to sort through table rows by filtering them based on the selections of multiple checkbox

How can I create interdependent logic for checkbox sections in a form to filter based on all selections? I am looking for help with my code snippet that showcases checkboxes controlling the visibility of table rows: $(document).ready(function() { $(" ...

Building Vertical Tabs with external JS for loading custom visuals

I am trying to figure out how to display the visuals I created in an alternate page within my Vertical tabs. The tabs are already styled, and I have omitted the CSS due to its length. The HTML file I am working with is test.html, and the visuals are in the ...